Page 1 of 1

How's my code? A loop question

Posted: Tue Dec 12, 2006 1:56 pm
by $var
Hi, I've been using this huge code for years to create list pages... with alternating cell backgrounds for multiple entries. I know i'm not calling the data properly in this sample, but the meat is really in the script anyways.

What are your thoughts, is there something lighter than this to count entries, and display them in a list?

Code: Select all

$sql = "SELECT COUNT(*) FROM table'";
                $numrecord = mysql_query($sql);
                $numrecords = mysql_fetch_array($numrecord);
               
                $intRecsPerPage=100;
                if($_POST["intpage"]=="")
                {
                        $intpage=1;
                }
                $sql = "SELECT * FROM table";
                $result = mysql_query($sql) or die (mysql_error());
       
                $totalpages = intval(($numrecords[0])/$intRecsPerPage);
                if(intval($numrecords[0]/$intRecsPerPage) != ($numrecords[0]/$intRecsPerPage))
        {
         $totalpages = $totalpages + 1;
        }

                for($x = (($intpage-1) * $intRecsPerPage); $x < (($intpage-1) * $intRecsPerPage) + $intRecsPerPage; $x++)
                {
                        if($x/2 != intval($x/2))
                                        {
                                                $bgcolor = "#ffffff";
                                        }
                                        else
                                        {
                                        $bgcolor = "#F7F7F7";
                                        }
                        if($x >= $numrecords[0])
                        {
                                break;
                        }
                $issueresults = mysql_fetch_array($result);
                //BEGIN LOOP
                <tr><td bgcolor=$bgcolor>sample</td></tr>
                //END LOOP
          }

Posted: Tue Dec 12, 2006 3:19 pm
by Ollie Saunders
Its not obvious to me what your script it doing, consider revising your variable names.
This may be helpful or useless to you:

Code: Select all

$q = 'SELECT * FROM TABLE';
$result = mysql_query($q) or die('Query Error');
$count = mysql_num_rows($result);
echo '<ul>';
for ($i =0; $row = mysql_fetch_assoc($result); ++$i) {
    if ($i % 2 == 0) {
        $style = '#eef';
    } else {
        $style = '#fff';
    }
    echo '<li style="background:' . $colour . '">' . $row['someField'] . '</li>';
}
echo '</ul>';
echo $count . ' rows displayed';

Posted: Tue Jan 02, 2007 4:29 pm
by $var
hhmm... okay, i'm confused with this code.

it's drawing only 7 of the 8 records in the database.
trying both ORDER BY ASC and DESC cuts off either the record #1 or #8

Code: Select all

$q = 'SELECT * FROM hcw_section ORDER BY Section_ID DESC';
$result = mysql_query($q) or die('Query Error');
$count = mysql_num_rows($result);

     for ($i =0; $row = mysql_fetch_assoc($result); ++$i) {
     if ($i % 2 == 0) {
	$style = '#eef';
     } else {
        $style = '#fff';
     }
     
     echo "$row[Section_Title]<br />";
     }

any ideas why it would not post all the records here?

Posted: Tue Jan 02, 2007 5:32 pm
by RobertGonzalez
Look at the limit in the for loop.

Posted: Tue Jan 02, 2007 5:36 pm
by pickle
Something even simpler (IMO)

Code: Select all

$q = 'SELECT * FROM hcw_section ORDER BY Section_ID DESC';
$result = mysql_query($q) or die('Query Error');
$style = '#eef';
while($row = mysql_fetch_assoc($result))
{
  $style = ($style == '#eef') ? '#fff' : '#eef';
  echo $row[Section_Title];
}

Posted: Tue Jan 02, 2007 5:56 pm
by Ollie Saunders
Quote your array keys please. ['Section_Title'] not [Section_Title].
I'm quite sure there is nothing wrong with the loop. Test your query directly with a db client (command line one is always a winner) and make sure the data you are expecting actually exists.