Jump to content

ive searched with no luck. first record not displaying


black.horizons

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!
Link to comment
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.
Link to comment
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]
Link to comment
Share on other sites

  • 1 month later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.