Hello , Im having a problem displaying more then 1 result from a mysql_fetch_array statement in my php application.

// Start Fetch of Notes
$username = $_SESSION['username'];
$sql = "SELECT `to`, `from`, `time`, `note` FROM `shocc_notes` WHERE `to` = '$username'";
$query = mysql_query($sql) or die(mysql_error());
while($array = mysql_fetch_array($query)) {
	$to = $array['to'];
	$from = $array['from'];
	$note = $array['note'];
	$time = $array['time'];
}
if(!$array) {
	$no_new = "You have No New Notes ...";
}

That code is to pull private notes from SQL and display them , but it will only return 1 note.

How could i make the code return ALL the notes send to that member ... and why isnt it?

Thanks for your help

    $result = mysql_query("SELECT * FROM shocc_notes WHERE to = '" . $_SESSION['username'] . "'") or die(mysql_error());
    while ($row = mysql_fetch_assoc($result)) {$array[] = $row;}
    print_r($array);
    

      I cant do a print_r b/c im using smarty , i must assign the output

      when i try this

      $result = mysql_query("SELECT * FROM `shocc_notes` WHERE `to` = '" . $_SESSION['username'] . "'") or die(mysql_error());
      while ($row = mysql_fetch_assoc($result)) {$array[] = $row;}
      $to = $array['to'];
      if(!$array) {
      	$no_new = "You have No New Notes ...";
      }
      
      // Assign The Data for Output
      $smarty->assign('no_new',$no_new);
      $smarty->assign('to',$to);
      $smarty->assign('from',$from);
      $smarty->assign('note',$note);
      $smarty->assign('time',$time);
      

      The "TO" isnt returning any results , i didnt bother assigning the rest of them untill i was sure that the TO was working , and its not , is their any other way to do this?

        Sayian wrote:

        I cant do a print_r b/c im using smarty

        my code was just a simple example. the print_r() was just to illustrate to you how the array is being populated from the query result set.

          wont the original code i posted using mysql fetch array populate an array the same ?

          I dont understand what im doing wrong to not be able to get any data out

            no, your original code overwrites the variables $to, $from etc with every row retrieved.
            use $to[] = $array('to'); to create an array.
            Then you can cycle through those arrays via {foreach} or {section}.

            devinemke's solution can be used in smarty too, and I'd prefer that one because you have all the data that belongs together in a single array. It can be processed by smarty too and you don't have to change your code and template a lot in case you want to add another column.

              Ok , thanks i read the manual about for each statements

              Although im confused about how i would apply this

              Where would this foreach statement go?

              In the manual it displays somthing like foreach( $var as $var ) but im still confused how and where this would be used in my code

                that {foreach} was for smarty. Like this:

                 $result = mysql_query("SELECT * FROM shocc_notes WHERE to = '" . $_SESSION['username'] . "'") or die(mysql_error());
                while ($row = mysql_fetch_assoc($result)) {$notes[] = $row;}
                $smarty->assign('Notes', $notes);
                

                then you do the magic in smarty:

                Notes:
                {foreach item=$entry from=$Notes}
                $entry.to  $entry.from  $entry.note  $entry.time
                {/foreach}
                

                Then wrap a proper table around those fields and you should be set. If you want less control of the output and just print every field available, you can do a foreach with two dimensions as described in the smarty docs here: http://smarty.php.net/manual/en/language.function.foreach.php (Example 7-5).

                  Thanks for your help and patience ... i feel like a idiot all over again 😕

                  Now im getting this error from smarty
                  Fatal error: Smarty error: [in notes.tpl line 27]: syntax error: 'foreach: item' must be a variable name (literal string) (Smarty_Compiler.class.php, line 1158) in /usr/local/lib/php/Smarty/Smarty.class.php on line 1088

                  Ive made a table ... and placed well ... ill show you

                  
                  // THIS IS THE PHP CODE FROM NOTES.PHP
                  
                  $result = mysql_query("SELECT * FROM `shocc_notes` WHERE `to` = '" . $_SESSION['username'] . "'") or die(mysql_error());
                  while ($row = mysql_fetch_assoc($result)) {$notes[] = $row;}
                  
                  if(!$row) {
                  	$no_new = "You have No New Notes ...";
                  }
                  
                  // Assign The Data for Output
                  $smarty->assign('notes',$notes);
                  $smarty->assign('no_new',$no_new);
                  
                  

                  And now for the template ...

                  <p>Notes Inbox </p>
                  </span>
                    <p class="textmain">Welcome to Your Private Notes Center </p>
                    <p class="textmain">{foreach item=$entry from=$notes}</p>
                    <form name="notes" method="post" action="/send_note.php">
                      <table width="100%"  border="0">
                        <tr>
                          <td width="72%" valign="bottom">&nbsp;</td>
                          <td width="72%" valign="bottom">&nbsp;</td>
                          <td width="72%" valign="bottom">&nbsp;</td>
                          <td width="28%" rowspan="3" align="center" valign="top"><img src="/advertisement.gif" width="120" height="10"><br>
                            {$rm_160}</td>
                        </tr>
                        <tr>
                          <td valign="top"><p>{$entry.to}</p>
                          <td valign="top">{$entry.from}</td>
                          <td valign="top">{$entry.time}</td>
                        </tr>
                        <tr>
                          <td valign="top">&nbsp;</td>
                          <td valign="top">&nbsp;</td>
                          <td valign="top">&nbsp;</td>
                        </tr>
                      </table>
                    </form>  <p class="textmain"><span class="textmain">
                  
                  {/foreach}</span></p></td>
                  

                  Is that not correct , bc it's giving me an error

                    whops, sorry, that line should read:

                      <p class="textmain">{foreach item=entry from=$notes}</p>
                    

                    Did that fix it?

                      Yes , the error has gone away , but once again no data is being outputted

                        In this code ...

                        I built a table ... placed the values in the cells ...

                        but somthing strang is happening

                        in the cell marked Test!

                        the words Test! wont showup since ive put that foreach statement inside ...

                        also no data is being displayed at all ...

                        <tr>
                          <td align="center" valign="top" class="smallcontent">&nbsp;</td>
                        <td><span class="textmainbold">
                        <p>Notes Inbox </p>
                        </span>
                          <p class="textmain">Welcome to Your Private Notes Center ff </p>
                          <p class="textmain">{foreach item=entry from=$notes}</p>
                          <table width="100%"  border="0">
                            <tr>
                              <td>$entry.to</td>
                              <td>$entry.from</td>
                              <td>$entry.note</td>
                            </tr>
                          </table>  
                        <p class="textmain">test!</p> <p class="textmain"><span class="textmain">{/foreach}</span></p></td> </tr>

                        Im not a newbie looking for answer's ..... I just truley dont understand what is going on and why

                          Ok , im just going to focus on getting the TO , displaying

                          so ive changed the template code to this

                          <table width="100%"  border="0">
                            <tr>
                              <td>{foreach item=entry from=$notes} </td>
                              <td>$entry.to</td>
                              <td>{/foreach}</td>
                            </tr>
                          </table>
                          

                          But even the TO part wont show , so does that mean theirs somthing wrong with the php code? in notes.php?

                            'test!' not being indicates that your $notes array isn't filled properly somehow. Enable the Smarty-Debug console ($smarty->debugging = true; ) or do a var_dump($notes) in the script.

                            Also in the first part of your script, $no_notes will always be set. Change

                            while ($row = mysql_fetch_assoc($result)) {$notes[] = $row;}
                            
                            if(!$row) {
                                $no_new = "You have No New Notes ...";
                            }
                            

                            to

                            if (mysql_num_rows($result)) {
                                while ($row = mysql_fetch_assoc($result)) {$notes[] = $row;}
                            } else {
                                $no_new = "You have No New Notes ...";
                            }
                            

                              That small php code change that you mentioned above did the trick for the whole fair!

                              Only problem is .. the note's are populating in the table sideways , instead of adding a new row everytime .. do i just need to add a <br /> to each variable of is their another magic smarty statement that will make them drop down rows instead of move sideways

                                the usual design is (leaving all css stuff away):

                                <table>
                                {foreach item=entry from=$notes}
                                <tr>
                                  <td>{$entry.to}</td>
                                  <td>{$entry.from}</td>
                                </tr>
                                {/foreach}
                                </table>
                                

                                That should give you a table where you have each entry in a new row.

                                  Write a Reply...