I'm trying to get this code to spit out 5 columns and 2 row of data. Instead I'm getting 2 columns and 5 rows. Can anyone help me out with a quick solution. I've tried everything I could think of. I know it's probably obvious, but I just can't see it right now. Here's the code: $count = 1; $column = 1; if ($column == 1) { printf("<tr><td><b>$count</b>%s</td>",$imagepath); } else{ //this is the column 2 printf("<td><b>$count</b>%s</td></tr>",$imagepath); } $count += 1; // this is a modulus operator it gets the remainder of the equation $column = $count % 2; Code (markup): Thanks
The "if" statement only runs once and its totally unnecessary as well. Since you say column=1, the if statement is guaranteed to be true (column == 1), then it finishes and makes the second column. I'd think you want to make a for loop or something. (Its been a VERY long time since I did programming but this looks very similar to C) For column=1 to 5 step 1; then make your table data
Your code here. $count = 1; $column = 1; if ($column == 1) { printf("<tr><td><b>$count</b>%s</td>",$imagepath); } else{ //this is the column 2 printf("<td><b>$count</b>%s</td></tr>",$imagepath); } $count += 1; // this is a modulus operator it gets the remainder of the equation $column = $count % 2; Code (markup): Is identical to this: $count = 1; $column = 1; printf("<tr><td><b>$count</b>%s</td>",$imagepath); $count += 1; printf("<td><b>$count</b>%s</td></tr>",$imagepath); // this is a modulus operator it gets the remainder of the equation $column = $count % 2; Code (markup): Unless you've left out code, the else statement is probably ignored every time the code runs. In order to make it work, after this line: $count += 1; You'd have to run the if statement again. So, you may as well just make a do/for loop or whatever. to increment count automatically (its cleaner too) and stop when count equals the target number.
It's not ignored, it does output 2 columns ; 5 rows. I'm trying to get it to output 5 columns ; 2 rows. I tried to 'for' loop, but no luck, as i'm lost with it.
Try it. $count = 1; $column = 1; printf("<tr>"); if ($column == 1) { printf("<td><b>$count</b>%s</td>",$imagepath); } printf("</tr><tr>"); else{ //this is the column 2 printf("<td><b>$count</b>%s</td>",$imagepath); } printf("</tr>"); $count += 1; // this is a modulus operator it gets the remainder of the equation $column = $count % 2; Code (markup):
Here's my code. 5 columns: column = 1; printf("<tr>"); do { printf("<td><b>$column</b>%s</td>",$imagepath); $column += 1; } while (column < 5); printf("</tr>"); Code (markup): That'd make 1 row, 5 columns wide. Then I presume you are following it with a second loop to make the data for the columns.
here is the correct code : for($row=1; $row<=2; $row++){ $column=1; printf("<tr>"); for($column=1; $column<=5; $column++){ $count = $column; printf("<td><b>$count</b>%s</td>",$imagepath); } printf("</tr>"); }
http://www.w3schools.com/php/php_looping.asp Mine seems to work according to this, with minor changes... $column = 1; printf("<tr>"); do { printf("<td><b>$column</b>%s</td>",$imagepath); $column++; } while ($column < 5); printf("</tr>"); Code (markup): But this is why I hire people to make stuff for me
It's PHP. scriptur, That did display 5 columns, but it also displayed 2 rows of each image. So it was 5 columns + 2 rows of each image. Therefor there are 20 rows.
It seems the code snippet you gave is already in a loop, thats why you are getting 20 rows. Please post the code snippet including the loop itself
Here is the entire code. <?php include("includes/dbaccess2.php"); $npage = 1; $pp = 10; $s = 1; $spage = (($npage-1)*$pp) + 1; $fpage = $spage + ($pp-1); $count = 1; $column = 1; $sort = ''; switch ($s) { case 1: //Latest adds $sort = " ORDER BY `crtime` DESC"; break; case 2: //most viewd $sort = " ORDER BY `hits` DESC"; break; } $sql = "SELECT * FROM `table` {$sort}"; $result = mysql_query($sql); $currit = 0; if ($_GET['ID'] == ""){ while($row = mysql_fetch_array($result)) { if($_GET['cat'] == "" || $_GET['cat'] == $row['cat_id']){ $currit++; if($currit <= $fpage && $currit >= $spage){ $grabber = $row['cat_id']; if($row['author'] == "") { $author = "Administrator"; } else { $author = $row['author']; } $nurl = "?ID=".$row['id']; $iurl = "?ID=".$row['id']; $imagepath = '<td width="150" align="center" class="text"><a title="'.$row['title'].'" href="'.$nurl.'"><img border="0" src="'.$row['thumbnail'].'" width="105" height="75"/></a><br/>'.$row['title'].'</td>'; for($row=1; $row<=2; $row++){ $column=1; printf("<tr>"); for($column=1; $column<=5; $column++){ $count = $column; printf("<td><b>$count</b>%s</td>",$imagepath); } printf("</tr>"); } } } } } mysql_close($conn); ?> </table> Code (markup):
here is the fix below. notice that I have removed the <td> tags from the output of the image because the image is already enclosed in these tags <?php include("includes/dbaccess2.php"); $npage = 1; $pp = 10; $s = 1; $spage = (($npage-1)*$pp) + 1; $fpage = $spage + ($pp-1); $count = 1; $column = 1; $sort = ''; switch ($s) { case 1: //Latest adds $sort = " ORDER BY `crtime` DESC"; break; case 2: //most viewd $sort = " ORDER BY `hits` DESC"; break; } $sql = "SELECT * FROM `table` {$sort}"; $result = mysql_query($sql); $currit = 0; if ($_GET['ID'] == ""){ $count = 1; //scriptur printf("<tr>"); //scriptur while($row = mysql_fetch_array($result)) { if($_GET['cat'] == "" || $_GET['cat'] == $row['cat_id']){ $currit++; if($currit <= $fpage && $currit >= $spage){ $grabber = $row['cat_id']; if($row['author'] == "") { $author = "Administrator"; } else { $author = $row['author']; } $nurl = "?ID=".$row['id']; $iurl = "?ID=".$row['id']; $imagepath = '<td width="150" align="center" class="text"><a title="'.$row['title'].'" href="'.$nurl.'"><img border="0" src="'.$row['thumbnail'].'" width="105" height="75"/></a><br/>'.$row['title'].'</td>'; //begin scriptur if( ($count % 5) == 0 ){ printf("</tr><tr>"); } printf($imagepath); $count += 1; //end scriptur }//end if }//end if } //end while printf("</tr>"); //scriptur } mysql_close($conn); ?> </table>__________________ Code (markup):