display x amount of columns wide

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
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

display x amount of columns wide

Post by Jim_Bo »

Hi,

The following code works, I have just pulled out the part that display x amount of columns wide without the pagination .. Im sure I dont have the x amount of columns code correct altho it seems to be working.

Is it correct?

Code: Select all

<table border="0" cellspacing="0" cellpadding="10" align="center">

<?php

if($_GET['page'] == NULL) { 
    $page = 1; 
} else { 
    $page = $_GET['page']; 
} 

	$max_results = 8; 
	$numcols = 4;
	$numcolsprinted = 0;

	$from = (($page * $max_results) - $max_results); 

	$count = mysql_query("SELECT * FROM gallery_photos"); 
	$sql = mysql_query("SELECT * FROM gallery_photos WHERE photo_category='".$_GET['cid']."' LIMIT $from, $max_results");
	$totalEntries = mysql_num_rows($count);
		while($row = mysql_fetch_array($sql)) {

	if($numcolsprinted == $numcols) {
	echo "<tr>\n</tr>\n";
		$numcolsprinted = 0;
}

echo '<td>';

if(mysql_num_rows($result) == 0) {
                        
	echo '<font class="txt"><b>No images in this category yet</b></font>';
} else {
	echo '<a href="../index.php?pages=sh_image&pid='.$row['photo_id'].'&page='.$_GET['page'].'"><img src="'.$images_dir.'/tb_'.$row['photo_filename'].'" border="1"></a>';

if ($_SESSION['user_level'] == 3) {

	echo '<br><font class="txt"><a href="../index.php?pages=edit_photo&category_name='.$category_name.'&photo_id='.$row['photo_id'].'">Edit/Delete</a></font>';
 }
}

echo '</td>';

$numcolsprinted++;

}

$colstobalance = $numcols - $numcolsprinted;
for ($i=1; $i<=$colstobalance; $i++) {

} 
echo "<td></td>\n";
?> 

</table>
Thanks
ruchit
Forum Commoner
Posts: 53
Joined: Mon Sep 26, 2005 6:03 am

Post by ruchit »

code seems to be OK more or less.. but u can improve on it

Code: Select all

if(mysql_num_rows($result) == 0) {
                        
    echo '<font class="txt"><b>No images in this category yet</b></font>';
} else {
should come above while loop rather than in it.
Also, for getting the count.. it would have been better, if you used

Code: Select all

"SELECT count(id) FROM categories"
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post by Jim_Bo »

Hi,

Exactly where should

if(mysql_num_rows($result) == 0) {

echo '<font class="txt"><b>No images in this category yet</b></font>';
} else {

go .. where it is at present it doesnt even work

Thanks
ruchit
Forum Commoner
Posts: 53
Joined: Mon Sep 26, 2005 6:03 am

Post by ruchit »

Code: Select all

if(! mysql_num_rows($result)) {
		echo '<font class="txt"><b>No images in this category yet</b></font>';
} 
else {
	while($row = mysql_fetch_assoc($sql)) { //mysql_fetch_assoc is faster than mysql_fetch_array
	
		if($numcolsprinted == $numcols) {
			echo "<tr>\n</tr>\n";
			$numcolsprinted = 0;
		}
	
		echo '<td>';
		
		echo '<a href="../index.php?pages=sh_image&pid='.$row['photo_id'].'&page='.$_GET['page'].'"><img src="'.$images_dir.'/tb_'.$row['photo_filename'].'" border="1"></a>';
		
		if ($_SESSION['user_level'] == 3) {
		
			echo '<br><font class="txt"><a href="../index.php?pages=edit_photo&category_name='.$category_name.'&photo_id='.$row['photo_id'].'">Edit/Delete</a></font>';
		}
		
		echo '</td>';
	}
}
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post by Jim_Bo »

Hi,

That code gives produces the same result .. still no promt on

if(! mysql_num_rows($result)) {
echo '<font class="txt"><b>No images in this category yet</b></font>';


Thanks
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

add - print mysql_num_rows($result) - somewhere so that you can see that records are coming along.
ruchit
Forum Commoner
Posts: 53
Joined: Mon Sep 26, 2005 6:03 am

Post by ruchit »

OK, got it, try this

Code: Select all

echo '<td><font class="txt"><b>No images in this category yet</b></font></td>';
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post by Jim_Bo »

hi,

got it all working using:

Code: Select all

$max_results = 8; 
	$numcols = 4;
	$numcolsprinted = 0;

	$from = (($page * $max_results) - $max_results); 

	$count = mysql_query("SELECT count(id) FROM categories"); 
	$sql = mysql_query("SELECT * FROM gallery_photos WHERE photo_category='".$_GET['cid']."' LIMIT $from, $max_results");
		
if(mysql_num_rows($sql) == 0) {
	echo '<br><br><font class="txt"><b>No images in this category yet</b></font>';
	return; 
}

?>

<table border="0" cellspacing="0" cellpadding="10" align="center">

<?php

    while($row = mysql_fetch_assoc($sql)) {
     
	if($numcolsprinted == $numcols) { 
		echo "<tr>\n</tr>\n"; 
			$numcolsprinted = 0; 
} 
     
	echo '<td>'; 
         
	echo '<a href="../index.php?pages=sh_image&pid='.$row['photo_id'].'&page='.$_GET['page'].'"><img src="'.$images_dir.'/tb_'.$row['photo_filename'].'" border="1"></a>'; 
         
if($_SESSION['user_level'] == 3) { 

	echo '<br><font class="txt"><a href="../index.php?pages=edit_photo&category_name='.$category_name.'&photo_id='.$row['photo_id'].'">Edit/Delete</a></font>'; 
} 
         
	echo '</td>'; 

	$numcolsprinted++;
}

	$colstobalance = $numcols - $numcolsprinted;

	for($i=1; $i<=$colstobalance; $i++) {
} 

echo "<td></td>\n";

?> 

</table>

Thanks
ruchit
Forum Commoner
Posts: 53
Joined: Mon Sep 26, 2005 6:03 am

Post by ruchit »

good for you but there are <tr> tags except for

Code: Select all

if($numcolsprinted == $numcols) {
        echo "<tr>\n</tr>\n";
            $numcolsprinted = 0;
}
are you sure its a good practice?
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post by Jim_Bo »

Hi,

Not sure what you mean exactly, hence why I was asking if the column part of the script was correct ..


Cheers
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

I was going to post

Code: Select all

if (mysql_num_rows($result) = 0) {
should have been:

Code: Select all

if (mysql_num_rows($sql) < 1) {
But you already fixed it :p
Post Reply