Jump to content

Archived

This topic is now archived and is closed to further replies.

black.horizons

ive searched with no luck. first record not displaying

Recommended Posts

[code] <?php

   $topic_query="SELECT * FROM topics ORDER BY ID";
   /* Error occurred, return given name by default */
   $result=mysql_query($topic_query);
   $num_rows=mysql_numrows($result);
mysql_close();
   if(!$topic_query || ($num_rows < 0)){
      echo "Error displaying info";
      return;
   }
   if($num_rows == 0){
      echo "Database table empty";
      return;
   }
   /* Display table contents */
   $i=0;
   while ($i < $num_rows) {

   $id=mysql_result($result,$i,"ID");
   $topic_name=mysql_result($result,$i,"TopicName");

      if($i % 2) {
        echo "<tr class=\"tabletext\">";
      }
      else {
        echo "<tr class=\"tabletextwhite\">";
      }

   $i++;
   }
?>[/code]

its killing me. its only displaying the last record. and this is the second query i've had like this. the first i got rid of...because of the hassle. if i get this sorted i'll apply the same technique.

there should be "test1", "test2" and "test3" showing, where only "test3" is showing...any help!?!

[a href=\"http://www.catalyticstudios.haisoft.net/annadale/forum/index2.php\" target=\"_blank\"]http://www.catalyticstudios.haisoft.net/an...orum/index2.php[/a]

thats the page!

Share this post


Link to post
Share on other sites
instead of having:
[code]  /* Display table contents */
   $i=0;
   while ($i < $num_rows) {

   $id=mysql_result($result,$i,"ID");
   $topic_name=mysql_result($result,$i,"TopicName");

      if($i % 2) {
        echo "<tr class=\"tabletext\">";
      }
      else {
        echo "<tr class=\"tabletextwhite\">";
      }

   $i++;
   }[/code]

try:

[code]/*Display table contents */

while($data_array = mysql_fetch_array($result, MYSQL_ASSOC)){
$id = $data_array['ID'];
$topic_name = $data_array['TopicName'];      

     if($i % 2) {
        echo "<tr class=\"tabletext\">";
      }
      else {
        echo "<tr class=\"tabletextwhite\">";
      }
   echo $topic_name . "</tr>";
   $i++;
}[/code]

I don't see where you echo $topic_name, perhaps on another script. Wherever you did, delete it, and this will print the correct variables.

The MYSQL_ASSOC creates an associative array, which can be accessed using $array_name['fieldname']

In this case I named the array $data_array, but it can be called anything.

Each time it passes through the while loop, it takes the next row and uses that as the row to choose fields from.

Hope this helps.

Share this post


Link to post
Share on other sites
I'm amazed it's even displaying the last record. All the posted code does is echo <tr> tags.

Try
[code]   $topic_query="SELECT ID, topicName FROM topics ORDER BY ID";
  
   $result=mysql_query($topic_query);

   if(!$result){
      echo "Error displaying info";
      return;
   }
   $num_rows=mysql_num_rows($result);
   if($num_rows == 0){
      echo "Database table empty";
      return;
   }
   /* Display table contents */
   $i=0;
   echo '<table>';
   while (list($id, $topic_name)=mysql_fetch_row($result)) {

      if($i++ % 2) {
        echo "<tr class=\"tabletext\">";
      }
      else {
        echo "<tr class=\"tabletextwhite\">";
      }
      echo "<td>$id</td><td>$topic_name</td></tr>";
   }
   echo '</table>';[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.