Page 1 of 1
IF Statement under conditions [SOLVED]
Posted: Wed Jun 14, 2006 2:45 am
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!
Posted: Wed Jun 14, 2006 2:59 am
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)).
Posted: Wed Jun 14, 2006 3:00 am
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.
Posted: Wed Jun 14, 2006 3:09 am
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
Posted: Wed Jun 14, 2006 3:37 am
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 /> <strong>$condition</strong><br /> </td>";
} else { // resultset is empty
echo "<td valign=\"top\" align=\"middle\" width=\"100\"> </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
Posted: Fri Jun 16, 2006 9:40 pm
by tecktalkcm0391
Thanks it works!