How's my code? A loop question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
$var
Forum Contributor
Posts: 317
Joined: Thu Aug 18, 2005 8:30 pm
Location: Toronto

How's my code? A loop question

Post 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
          }
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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';
User avatar
$var
Forum Contributor
Posts: 317
Joined: Thu Aug 18, 2005 8:30 pm
Location: Toronto

Post 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?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Look at the limit in the for loop.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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];
}
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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.
Post Reply