SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Jun 2011
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    new message as a part of MVC's new view

    Hello there .

    I have a live chat application that I am trying to build using PHP and MySQL and I also try to use the MVC architectural pattern to it . So here is the place where I stuck .

    I successfuly authenticate my user based on sessions and now when they enter chat with their username
    they will see messages pulled out from the database like the followin
    1 kevin message...
    2jim message...
    3 kevin message...
    4 jim message...
    5 kevin message...
    6 kevin message...
    and under this messages I will have a text field and submit button to send the message . So here is my question . When someone submits a new message in order to apply MVC do I have to do
    "require_once(another_view)" which will contain the previous messages and also the last message that the
    user entered . How that "one new message stuff" is handled in terms of MVC's View . Hope you got the idea. Thanks in advance !!! Will be waiting .




    Please help me today to solve this problem and make me that happiest person of all the Christmas Time .

  2. #2
    SitePoint Evangelist stonedeft's Avatar
    Join Date
    Aug 2009
    Posts
    586
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What MVC Framework are you using?
    Don't Panic

  3. #3
    SitePoint Member
    Join Date
    Jun 2011
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I can submit the appliaction structure . I am not using not Zend nor Symphony, or something else

    If a user types a message and enteres "Send" I can just simply via JavaScript add that message to the rest of the messages but I have doubts that in terms of MVC it is correct because you change the view (because we are adding a new message) but from the other hand we dont do require_once(another_view)(which will contain updated messages). Since I am not strong in MVC . I will tell you that I understood MVC like way of coding where you will have controllers which will decide with what view to do require_once .
    So all my doubts are around that JavaScript that it is a view but we change the view without working any controller .
    I hope I explained something

    BTW I thought this . What if we just dont immediatly add the "sent " message to other messages ,and what if via ajax we will go to "index.php" where we will detect that message has come from the "Send burtton " and then will do require_once ?






    Here is what all I have for now .
    My application is incomlete but i'll list for you what I have .

    All the the application is inside "lc" folder
    and inside it I have 4 folders and the index.php file which looks like this:
    index.php
    Code:
    <?php session_start();
    
    	require_once "includes/config.php";
    	require_once "models/Db.php";
    
    	if(isset($_SESSION["username"])){
    		require_once "views/logged.php";						 //  view
    			
    	}else{
    		if(isset($_POST["username"]) && !empty($_POST["username"])){
    			$_SESSION["username"] = $_POST["username"];
    			$username = $_POST["username"];
    			require_once "views/logged_first.php";				//  view
    		}else{
    			require_once "views/register.php";					//  view
    	}
    
    
    }
    
    
    
    ?>
    Inside the first "includes" folder I have for now only "config.php"
    Code:
    config.php
    Code:
    <?php
    	defined("DB_HOST") ? null : define("DB_HOST","localhost");
    	defined("DB_USER") ? null : define("DB_USER","root");
    	defined("DB_PASS") ? null : define("DB_PASS","sky");
    	defined("DB_NAME") ? null : define("DB_NAME","livechat");
    
    ?>

    The Models folder includes "Db.php" file
    Db.php
    Code:
    <?php
    	class Db{
    		public $connection;
    		public $selected_db;
    		private  static $instance;
    		
    		private function __construct(){
    			$this->connection = mysql_connect(DB_HOST,DB_USER,DB_PASS);
    			if(!$this->connection){
    				die("Couldnt connect  to the database ".mysql_error());
    			}else{
    				echo "Successfuly connected to the database .<br/>";
    			}
    			
    			$this->selected_db = mysql_select_db(DB_NAME,$this->connection);
    			if(!$this->selected_db){
    				die("Couldnt select a database ".mysql_error());
    			}else{
    				echo DB_NAME." database successfuly is chosen. <br/>";
    			}
    			
    			$query = "SELECT * FROM chat WHERE id<14";
    			$result = mysql_query($query);
    			while($row =mysql_fetch_array($result)){
    				echo $row["id"]. " ".$row["user"]." ".$row["time"]." ".$row["message"]."<br/>";
    			}
    		}// End "__construct()" method 
    		public static function getSingleton(){
    			if(!self::$instance instanceof self){
    				self::$instance = new self();
    			}
    			return self::$instance;
    		} // END "getSingleton()" method
    		
    	} // End "Db"  class
    
    
    
    
    ?>



    The controller folder is not ready since for now the controller's function does the index.php file . After I will separate it and put it in the controller



    In the "views" folder I have
    3 files for now
    register.php
    Code:
    <html>
        <head>
    
        </head>
        <body>
           <form action="index.php" method="POST">
                Nickname: &nbsp<input type="text" name="username">
                <input type="submit" value="Enter" name="submit" > 
           </form>
    
        </body>
       
    </html>

    logged.php
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    	"http://www.w3.org/TR/html4/loose.dtd">
    <html>
    	<head>
    		<title>Basic</title>
    	</head>
    	<body>
    		<?php
    				echo "<h3>Welcome back ". $_SESSION["username"]. "</h3> "."<br/>";
    				$db = Db::getSingleton();
    		
    		?>
    	</body>
    </html>

    and this which is not that much important
    logged_first.php
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    	"http://www.w3.org/TR/html4/loose.dtd">
    <html>
    	<head>
    		<title>Basic</title>
    		<script type="text/javascript"></script>
    	</head>
    	<body>
    	<?php
    		echo "<h3>Logged for the first time: ----Welcome----</h3><br/><br/>";
    		echo "Logged in as : ".$_SESSION["username"];
    		$db = Db::getSingleton();
    		
    	?>
    	<form >
    		<input type="text" name="message">
    		<input type="button" value="Send" onclick="addMessage()" id="send">
    	</form>
    	</body>
    </html>

    Please just dont pay attention on that my __construct outputs messages I'll orginize it in the way it is supposed to .
    [/quote]

  4. #4
    SitePoint Member
    Join Date
    Jun 2011
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This forum is a MESS. A typical example of GARBAGGE . 2 days ago posted a question and explained what I have still was desparately waiting but withoud any result . When I read your book it says that our community is a vibrant and awesome . This is what it meant ? This is not happening with me for the first time here in this forum . A JUNK PLACE to the fill the posts to pretend like the question was answered . Learn to care the people that use the forums . I can listen reasons why I am incorrect since I am not a MESS and will give you a feedback .

  5. #5
    Founder of Primal Skill Ltd. feketegy's Avatar
    Join Date
    Aug 2006
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you take to maybe improve on your question, and explain it a little bit in detail in the first time round, then you probably will get a decent answer too. People reading your question don't know what's going on in your head unless you explain it and write it down.

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    I think you got no replies containing answers because nobody could really understand what you were asking.

    Hope you got the idea. Thanks in advance !!! Will be waiting .
    Well, no we didn't. And you will be waiting, what? over the Christmas Holiday period - well it was pretty quiet on here I admit.

    I suspect English is not your mother tongue, and so it must be hard for you, we understand that. However from the tone of one of your replies you seem to be under the misconception that we are somehow your code slaves. This is clearly not the case.

    Overall though, you probably did not get any replies because while you claim to have grasped MVC, and hats off to you for doing that, you seem not to have worked out how to debug your own application, or have ever read How to ask questions the smart way.

    Specifically, you did not show:

    The error messages are you getting
    What have you tried that did not work
    If you echo or var_dump() your variables, what did not match your expectations?

    Also your questions seem to emanate from Javascript issues, you have a form, but no submit button -- why not try and get a straight HTML/PHP/Mysql version working first, then add JS niceties when you know the basic idea works?

  7. #7
    SitePoint Member
    Join Date
    Jun 2011
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ooohhhoooo hey guys thanks for the advices. I think I honestly got a forum called dreamincode where I got the anwser . You know dear "Cups " if you really start becoming intersted in the qustion after hearing about you the truth(like you mentioned maybe var_dump(),maybe...) it is already awesome. And dear Mr. if you bring this
    Hope you got the idea. Thanks in advance !!! Will be waiting .
    . I would like you to be aware that the person that asked this question just meant that hopefully you undesrstood the question and if you have questions like
    What MVC Framework are you using?
    he is willing to answer .. Ok dear Mr..?
    Ok. Mr Respected Cups please could you explain this a bit carefully your
    However from the tone of one of your replies you seem to be under the misconception that we are somehow your code slaves. This is clearly not the case.
    before I leave this forum ? I'll be gratefull if you do .

  8. #8
    Founder of Primal Skill Ltd. feketegy's Avatar
    Join Date
    Aug 2006
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This forum is clearly isn't for you, especially if you found your answer elsewhere.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •