IF Statement under conditions [SOLVED]

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
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

IF Statement under conditions [SOLVED]

Post by tecktalkcm0391 »

So far I have this:

Code: Select all

//Connection and DB info
$result = mysql_query($sql) OR die(mysql_error()); 
$row_count = mysql_num_rows($result);

if($c==1 || $c==6 || $c==11 || $c==16 || $c==21 || $c==26 || $c==31 || $c==36 || $c==41 || $c==46){
			print "<tr>";
		} else { 
			print "";
		}
For the if command I want it to continue with the same pattern I have here on until the value > ( $row_count / 5 ). How could I make that happen. Would I just do:

Code: Select all

if(for(....)){
print "<tr>";
		} else { 
			print "";
		}
}
Help, and THANKS!
Last edited by tecktalkcm0391 on Fri Jun 16, 2006 9:43 pm, edited 1 time in total.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

First thing to ask is what are all of the possible values for $c? Do you really need that many if parameters?

For the second part you would loop and inside the loop do a comparison using if (something > (otherthing / 5)).
User avatar
bmcewan
Forum Commoner
Posts: 55
Joined: Wed Jun 02, 2004 7:19 am
Location: West Yorkshire, UK.

Post by bmcewan »

are you trying to draw a table?

if so you might want to consider a different approach.

Code: Select all

$cols = 5;
$rows = ceil($row_count / $cols);

for ($r=0;$r<$rows;$r++) {
    print '<tr>';

    for ($c=0;$c<$cols;$c++) {
        print '<td>some data here</td>';
    }

    print '</tr>';
}
Means you can loop through your results and draw a table regardless of the size of the results returned from your query.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

What I am trying to do is from viewtopic.php?p=271769#271769

where it says

Code: Select all

if($c==1 || $c==6 || $c==11 || $c==16 || $c==21 || $c==26 || $c==31 || $c==36 || $c==41 || $c==46){
is where I wanted that to go, I need all of the values becaues I am drawing a table, but i am making it a 5*however many rows it needs
User avatar
bmcewan
Forum Commoner
Posts: 55
Joined: Wed Jun 02, 2004 7:19 am
Location: West Yorkshire, UK.

Post by bmcewan »

ok, let me rewrite that a little using your code from that other thread as a reference so you can see it in context;

Code: Select all

if(mysql_num_rows($result)) { 
    echo "<table  cellpadding=\"8\" align=\"center\" border=\"0\">"; 
    echo "<tbody>\n";

    $cols = 5; // number of cells per row
    $rows = ceil(mysql_num_rows($result) / $cols); // calculate amount of table rows needed to parse all resultset rows

    for ($r=0;$r<$rows;$r++) { // draw table rows
        echo '<tr>'; 

        for ($c=0;$c<$cols;$c++) { // create 5 tble cells or however many is set in cols
            if ($row = mysql_fetch_assoc($result)) { // get the next row from the resultset
                $id = $row['ID']; 
	        $name = $row['Name']; 
	        $image = $row['Picture']; 
	        $condition = $row['Condition']; 
	        $description = $row['Description']; 
	        echo "<td valign=\"top\" align=\"middle\" width=\"100\"> 
	        <a onclick=\"openwin($id); return false;\" href=\"javascript:;\"> 
	        <img title=\"$description\" height=\"80\" alt=\"$description\" src=\"$image\" width=\"80\" border=\"1\" /></a><br /> 
	        $name<br />        &nbsp;<strong>$condition</strong><br />  </td>";
             } else { // resultset is empty
                 echo "<td valign=\"top\" align=\"middle\" width=\"100\">&nbsp;</td>";
             }
         } 

    echo '</tr>'; 
    }
    echo "</table>\n"; 
} else { 
    echo "You do not have any items!\n"; 
}
it is cleaner than using a bunch of if statements and you can use the for loop to iterate through your db results.

hope this helps
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

Thanks it works!
Post Reply