Jump to content


Photo

ive searched with no luck. first record not displaying


  • Please log in to reply
4 replies to this topic

#1 black.horizons

black.horizons
  • Members
  • PipPipPip
  • Advanced Member
  • 78 posts

Posted 24 June 2006 - 10:42 PM

<?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++;
   }
?>

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!

#2 jfee1212

jfee1212
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 24 June 2006 - 11:31 PM

instead of having:
  /* 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++;
   }

try:

/*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++;
}

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.

#3 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,023 posts

Posted 24 June 2006 - 11:40 PM

I'm amazed it's even displaying the last record. All the posted code does is echo <tr> tags.

Try
   $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>';

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#4 jfee1212

jfee1212
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 25 June 2006 - 12:12 AM

Is the updated script uploaded to your site?

#5 black.horizons

black.horizons
  • Members
  • PipPipPip
  • Advanced Member
  • 78 posts

Posted 15 August 2006 - 01:02 PM

fortunately it now works. i spent ages working on other things, and got it to work. thanks again!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users