ofmyst Posted August 8, 2008 Share Posted August 8, 2008 My last problem!? I have a mysql database with painting info. I use php to pull the information. HOWEVER, I only want THREE images per line, right now it will put as many in the line as I have in the database. I thought perhaps the best option was an IF statement. I have an ID field set up, ID = T01, T02, T03 etc. Is there a way to say If ID = T01 - 03 then, if ID = T04 - 06, etc? Or is there a better way to structure this? Here is what it currently looks like: mysql_select_db("paintings", $con); $result = mysql_query("SELECT * FROM pieces"); while($row = mysql_fetch_array($result)) { echo "<td align='center' width='27%'>"; echo "<font color='white'>" . $row['Image'] . "</font> "; echo " "; echo "<font color='white'>" . $row['Link'] . "</font> "; echo "<font color='white'>" . $row['Title'] . "</font> "; echo "<font color='white'>" . $row['Medium'] . "</font> "; echo "<font color='white'>" . $row['Size'] . "</font>"; echo "</td>"; } Thank you very much for any help you can offer. Quote Link to comment https://forums.phpfreaks.com/topic/118829-solved-php-mysql-problem-trying-to-solve-with-conditional-if-statement/ Share on other sites More sharing options...
DarkWater Posted August 8, 2008 Share Posted August 8, 2008 Woops, didn't even see this topic. I know how to do what you're saying, but I just want to make sure I provide you with the correct HTML in the loop. Can you post where you begin the <tr> and the <table>? Quote Link to comment https://forums.phpfreaks.com/topic/118829-solved-php-mysql-problem-trying-to-solve-with-conditional-if-statement/#findComment-611893 Share on other sites More sharing options...
Jabop Posted August 8, 2008 Share Posted August 8, 2008 I am bad at math but it's something like... $x=1; while($loop) { if ($x%3==0) { echo '</tr><tr>'; } $x++; } Quote Link to comment https://forums.phpfreaks.com/topic/118829-solved-php-mysql-problem-trying-to-solve-with-conditional-if-statement/#findComment-611898 Share on other sites More sharing options...
DarkWater Posted August 8, 2008 Share Posted August 8, 2008 Yeah, basically. Quote Link to comment https://forums.phpfreaks.com/topic/118829-solved-php-mysql-problem-trying-to-solve-with-conditional-if-statement/#findComment-611900 Share on other sites More sharing options...
wildteen88 Posted August 8, 2008 Share Posted August 8, 2008 My last problem!? I have a mysql database with painting info. I use php to pull the information. HOWEVER, I only want THREE images per line, right now it will put as many in the line as I have in the database. I thought perhaps the best option was an IF statement. I have an ID field set up, ID = T01, T02, T03 etc. Is there a way to say If ID = T01 - 03 then, if ID = T04 - 06, etc? Or is there a better way to structure this? Here is what it currently looks like: mysql_select_db("paintings", $con); $result = mysql_query("SELECT * FROM pieces"); while($row = mysql_fetch_array($result)) { echo "<td align='center' width='27%'>"; echo "<font color='white'>" . $row['Image'] . "</font> "; echo " "; echo "<font color='white'>" . $row['Link'] . "</font> "; echo "<font color='white'>" . $row['Title'] . "</font> "; echo "<font color='white'>" . $row['Medium'] . "</font> "; echo "<font color='white'>" . $row['Size'] . "</font>"; echo "</td>"; } Thank you very much for any help you can offer. You'd setup counter to do that, to do so you'd first need to initiate a variable, eg p$i = 0; This will go before your while loop. Now the rest of the code goes within your while loop. Which will be // if $i is dividable by 3, echo a new table row if($i%3 == 0) { echo "</tr>\n<tr>\n"; } // increment counter $i++; Your code with admendments echo '<table border="0" cellspacing="1" cellpadding="3"> <tr>'; // initiate counter $i = 0; while($row = mysql_fetch_array($result)) { echo '<td align="center" width="27%" style="color: white">'. $row['Image'] . "<br />\n". $row['Link'] . "<br />\n". $row['Title'] . "<br />\n". $row['Medium'] . "<br />\n". $row['Size'] . "</td>\n"; // if $i is dividable by 3, echo a new table row if($i%3 == 0) { echo "</tr>\n<tr>\n"; } // increment counter $i++; } echo '</tr> </table>'; Quote Link to comment https://forums.phpfreaks.com/topic/118829-solved-php-mysql-problem-trying-to-solve-with-conditional-if-statement/#findComment-611901 Share on other sites More sharing options...
Jabop Posted August 8, 2008 Share Posted August 8, 2008 See, I already said my math was no good 0, 1, 2, </tr><tr> $x=0 is what I should've done and not $x=1 oops! Quote Link to comment https://forums.phpfreaks.com/topic/118829-solved-php-mysql-problem-trying-to-solve-with-conditional-if-statement/#findComment-611903 Share on other sites More sharing options...
DarkWater Posted August 8, 2008 Share Posted August 8, 2008 Woops, I lied. Didn't think about it correctly. xD Quote Link to comment https://forums.phpfreaks.com/topic/118829-solved-php-mysql-problem-trying-to-solve-with-conditional-if-statement/#findComment-611904 Share on other sites More sharing options...
ofmyst Posted August 8, 2008 Author Share Posted August 8, 2008 DarkWater, thank you for helping. Here is the section including the entire table. I have the additional td's with the 8% in order to have spacers on either side of the images. They aren't entirely necessary. <table width=100%> <tr> <td width=8%> </td> <?php $con = mysql_connect(""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("paintings", $con); $result = mysql_query("SELECT * FROM pieces"); while($row = mysql_fetch_array($result)) $id=ID("D"); if ($id=="TD01,TD02,TD03") { echo "<td align='center' width='27%'>"; echo "<font color='white'>" . $row['Image'] . "</font><br>"; echo "<br>"; echo "<font color='white'>" . $row['Link'] . "</font><br>"; echo "<font color='white'>" . $row['Title'] . "</font><br>"; echo "<font color='white'>" . $row['Medium'] . "</font><br>"; echo "<font color='white'>" . $row['Size'] . "</font>"; echo "</td>"; } mysql_close($con); ?> <td width=8%> </td> </tr> </table> Quote Link to comment https://forums.phpfreaks.com/topic/118829-solved-php-mysql-problem-trying-to-solve-with-conditional-if-statement/#findComment-611929 Share on other sites More sharing options...
DarkWater Posted August 8, 2008 Share Posted August 8, 2008 I think wildteen88 already beat me to it. Go try his code and post back. Quote Link to comment https://forums.phpfreaks.com/topic/118829-solved-php-mysql-problem-trying-to-solve-with-conditional-if-statement/#findComment-611931 Share on other sites More sharing options...
ofmyst Posted August 8, 2008 Author Share Posted August 8, 2008 DarkWater/WildTeen- Thank you! You have me on the right track. It is working except that the first line is one image - then it does three to a line. I have something wrong. Here is what I have so far: <table width=100%> <tr> <?php $con = mysql_connect(""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("paintings", $con); $result = mysql_query("SELECT * FROM pieces"); // initiate counter $i = 0; while($row = mysql_fetch_array($result)) { echo '<td align="center" style="color: white">'. $row['Image'] . "<br />\n". $row['Link'] . "<br />\n". $row['Title'] . "<br />\n". $row['Medium'] . "<br />\n". $row['Size'] . "</td>\n"; // if $i is dividable by 3, echo a new table row if($i%3 == 0) { echo "</tr>\n <tr>\n"; } // increment counter $i++; } echo '</tr> </table>'; to see what it looks like, if you want: www.sharondross.com/images/tinker/tinker5.php Also, I would like to have a space between image row and link line. And then a space between the rows so they don't run together. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/118829-solved-php-mysql-problem-trying-to-solve-with-conditional-if-statement/#findComment-611994 Share on other sites More sharing options...
wildteen88 Posted August 8, 2008 Share Posted August 8, 2008 Yea, sorry $i = 0; is supposed to be $i = 1; Quote Link to comment https://forums.phpfreaks.com/topic/118829-solved-php-mysql-problem-trying-to-solve-with-conditional-if-statement/#findComment-611997 Share on other sites More sharing options...
DarkWater Posted August 8, 2008 Share Posted August 8, 2008 Ah, just as I figured. Try: $i=1; >_< Quote Link to comment https://forums.phpfreaks.com/topic/118829-solved-php-mysql-problem-trying-to-solve-with-conditional-if-statement/#findComment-611998 Share on other sites More sharing options...
ofmyst Posted August 8, 2008 Author Share Posted August 8, 2008 Yes! Thank you all! Quote Link to comment https://forums.phpfreaks.com/topic/118829-solved-php-mysql-problem-trying-to-solve-with-conditional-if-statement/#findComment-612002 Share on other sites More sharing options...
ofmyst Posted August 9, 2008 Author Share Posted August 9, 2008 I thought I could take your solution and figure on my own how to format it the way I needed it. Essentially I have a table with the first row four columns (the first three with info in them) and the fourth showing the first two paintings one on top of the other. Then start another row that spanned the four columns and showed the next three paintings, then new row next three, etc. I works somewhat but adds in an extra </tr> just before the second row starts (which I found by doing a view source). I cannot seem to find where my mistake is. I left the info in the first few columns out to keep it cleaner. <table width=100%> <tr> <td width=10%> </td> <td width=39%> </td> <td width=10%> </td> <td width=40%> <table> <tr> <?php $con = mysql_connect(""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("paintings", $con); $result = mysql_query("SELECT * FROM pieces"); // initiate counter $i = 2; while($row = mysql_fetch_array($result)) { echo '<td align="center" style="color: white">'. $row['Image'] . "<br><br />\n". $row['Link'] . "<br />\n". $row['Title'] . "<br />\n". $row['Medium'] . "<br />\n". $row['Size'] . "<br><br><br><br></td>\n"; // if $i is equal to , echo a new table row if($i == 2 ) { echo "</tr>\n <tr>\n"; } // if $i is equal less than 3, echo a new table row then end 2 ptg table if($i == 3 ) { echo "</tr>\n </table>\n </td>\n </tr>\n <tr>\n <td colspan=4>\n <table>\n <tr>\n"; } // if $i is dividable by 3, echo a new table row if($i%3 == 0) { echo "</tr>\n <tr>\n"; } // increment counter $i++; } echo '</tr> </table>'; mysql_close($con); ?> </td></tr></table> You can see what I came up with here -- > http://www.sharondross.com/images/tinker/test2.php I'm sorry to be such a bother. Quote Link to comment https://forums.phpfreaks.com/topic/118829-solved-php-mysql-problem-trying-to-solve-with-conditional-if-statement/#findComment-612173 Share on other sites More sharing options...
ofmyst Posted August 9, 2008 Author Share Posted August 9, 2008 Nevermind the last part - I finally got it figured out. Thanks again for all of the help! Quote Link to comment https://forums.phpfreaks.com/topic/118829-solved-php-mysql-problem-trying-to-solve-with-conditional-if-statement/#findComment-612239 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.