-Karl- Posted March 25, 2010 Share Posted March 25, 2010 Okay, I want to create a 3 by x box. _____ l_ l_ l_l l_ l_ l_l l_ l_ l_l l_ l_ l_l So in html that would be (roughly) <table> <tr> <td> </td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td> </td> </tr> </table> Each td would be inputted with data from the database. However, I only want it 3 by x. What would be the correct way to do it? I have a MySQL table with around 13 fields in it which would produce 13 boxes, however, I only want 3 displayed on each line. Thanks for any help, I've tried to explain the best I can. Quote Link to comment https://forums.phpfreaks.com/topic/196444-creating-a-table-via-database-records/ Share on other sites More sharing options...
oni-kun Posted March 25, 2010 Share Posted March 25, 2010 foreach mysql_num_rows Quote Link to comment https://forums.phpfreaks.com/topic/196444-creating-a-table-via-database-records/#findComment-1031435 Share on other sites More sharing options...
-Karl- Posted March 25, 2010 Author Share Posted March 25, 2010 I've tried this: if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("****", $con); // Collect guides $query = "SELECT * FROM `names` ORDER BY name"; $run = mysql_query($query); if($run){ while($arr = mysql_fetch_assoc($run)){ if (($run)||(mysql_errno == 0)) { echo "<table width='100%' border='0' cellspacing='0' cellpadding='0'>"; if (mysql_num_rows($run)>0) { //display the data while ($rows = mysql_fetch_array($run,MYSQL_ASSOC)) { echo "<tr>"; //loop thru the serials to <strong class="highlight">create</strong> three columns $i = 0; while ($i < '3') { echo "<td>{$arr['name']}</td>"; $i++; } echo "</tr>"; } }else{ echo "<tr><td colspan='" . ($i+1) . "'>No Results found!</td></tr>"; } echo "</table>"; }else{ echo "Error in running query :". mysql_error(); } } } However, it's just echo'ing the same name over and over. Quote Link to comment https://forums.phpfreaks.com/topic/196444-creating-a-table-via-database-records/#findComment-1031437 Share on other sites More sharing options...
xcandiottix Posted March 25, 2010 Share Posted March 25, 2010 Try this: echo '<table width="100%" border="0">'; $data = mysql_query("SELECT * FROM table") or die(mysql_error()); $rows = mysql_num_rows($data); while($info = mysql_fetch_array($data)) { echo "<tr>"; echo '<th width="11%" scope="col">' . $info['field1'] . '</th>'; echo '<th width="11%" scope="col">' . $info["field2"] . '</th>'; echo '<th width="11%" scope="col">' . $info["field3"] . '</th>'; echo "</tr>"; } echo "</table>"; Quote Link to comment https://forums.phpfreaks.com/topic/196444-creating-a-table-via-database-records/#findComment-1031443 Share on other sites More sharing options...
-Karl- Posted March 25, 2010 Author Share Posted March 25, 2010 Why does this output: Warning: Invalid argument supplied for foreach() foreach ($rows as $value) { That's the line it's talking about. $rows = mysql_num_rows($run); foreach ($rows as $value) { echo "<td>$value {$arr['name']}</td>"; } Quote Link to comment https://forums.phpfreaks.com/topic/196444-creating-a-table-via-database-records/#findComment-1031446 Share on other sites More sharing options...
oni-kun Posted March 25, 2010 Share Posted March 25, 2010 "mysql_num_rows" is the number of rows to feed into the table, to make it variable length. It doesn't list the column names. Quote Link to comment https://forums.phpfreaks.com/topic/196444-creating-a-table-via-database-records/#findComment-1031449 Share on other sites More sharing options...
-Karl- Posted March 25, 2010 Author Share Posted March 25, 2010 The code xcandiottix posted is kind of what I'm looking for but it prints out all the info three times. I've messed around with it a bit, but I can't get it to work the way I want it to. <?php $con = mysql_connect("localhost","****","****"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("****", $con); // Collect guides echo '<table width="100%" border="0">'; $data = mysql_query("SELECT * FROM `names` ORDER BY name") or die(mysql_error()); $rows = mysql_num_rows($data); $i = 0; while($info = mysql_fetch_array($data, MYSQL_NUM) && ($i < '3')) { echo "<tr>"; echo '<td width="11%" scope="col">' . $info[0] . '</td>'; echo "</tr>"; $i++; } echo "</table>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/196444-creating-a-table-via-database-records/#findComment-1031451 Share on other sites More sharing options...
xcandiottix Posted March 25, 2010 Share Posted March 25, 2010 Isn't this statement you have: while ($i < '3') making it print it out 3 times? I think you should change that to while ($i < '1') just noticed that real quick, not sure if that's right. Quote Link to comment https://forums.phpfreaks.com/topic/196444-creating-a-table-via-database-records/#findComment-1031452 Share on other sites More sharing options...
-Karl- Posted March 25, 2010 Author Share Posted March 25, 2010 I have echo '<table width="100%" border="0">'; $data = mysql_query("SELECT * FROM `names` ORDER BY name") or die(mysql_error()); $rows = mysql_num_rows($data); $i = 0; while($i < '3') { while($info = mysql_fetch_array($data, MYSQL_NUM)) { echo "<tr>"; echo '<td width="11%" scope="col">' . $info[0] . '</td>'; echo '<td width="11%" scope="col">' . $info[1] . '</td>'; echo '<td width="11%" scope="col">' . $info[2] . '</td>'; echo "</tr>"; $i++; } } It's printing out name | info1 | info2 ------------------------ name | info1 | info2 ------------------------ name | info1 | info2 ------------------------ name | info1 | info2 Each row is a different row from the database, but this is not how I want it. I want: name info1 info2 | name info1 info2 | name info1 info2 -------------------------------------------------------------------- name info1 info2 | name info1 info2 | name info1 info2 and so on. Each box should have all the information in, and 3 rows per line. I just can't figure out a logical way to do it. Here's an image, I'm better at visualization so it may help you too: Quote Link to comment https://forums.phpfreaks.com/topic/196444-creating-a-table-via-database-records/#findComment-1031456 Share on other sites More sharing options...
-Karl- Posted March 25, 2010 Author Share Posted March 25, 2010 $i = 0; while ($i < '3') { while($info = mysql_fetch_array($data, MYSQL_NUM)) { echo '<td width="11%" scope="col">' . $info[0] . '</td>'; echo '<td width="11%" scope="col">' . $info[1] . '</td>'; echo '<td width="11%" scope="col">' . $info[2] . '</td>'; } $i++; } This works, it's printing out each info in the correct boxes, but it's listing them all horizontally and it's not doing the 3 by x Quote Link to comment https://forums.phpfreaks.com/topic/196444-creating-a-table-via-database-records/#findComment-1031458 Share on other sites More sharing options...
xcandiottix Posted March 25, 2010 Share Posted March 25, 2010 So, you'd want to echo out: <table width="50%" border="0" cellpadding="0"> <tr> <td>.$info['row 1'].</th> <td>.$info['info for row 1'].</th> <td>.$info['name for row 1'].</th> </tr> <tr> <td>.$info['row 2'].</th> <td>.$info['info for row 2'].</th> <td>.$info['name for row 2'].</th> </tr> <tr> <td>.$info['row 3'].</th> <td>.$info['info for row 3'].</th> <td>.$info['name for row 3'].</th> </tr> </table> You may want to put your i++ back in and then do this: $i=0 $var1 = 'row'.$i.'' $var1 = 'info for row'.$i.'' $var1 = 'name for row'.$i.'' <td>.$info['$var1'].</th> <td>.$info['$var2'].</th> <td>.$info['$var3'].</th> $i++ Quote Link to comment https://forums.phpfreaks.com/topic/196444-creating-a-table-via-database-records/#findComment-1031459 Share on other sites More sharing options...
-Karl- Posted March 25, 2010 Author Share Posted March 25, 2010 Mm, I don't understand what you mean. Quote Link to comment https://forums.phpfreaks.com/topic/196444-creating-a-table-via-database-records/#findComment-1031460 Share on other sites More sharing options...
xcandiottix Posted March 25, 2010 Share Posted March 25, 2010 echo '<table width="100%" border="0">'; $data = mysql_query("SELECT * FROM `names` ORDER BY name") or die(mysql_error()); $rows = mysql_num_rows($data); while($info = mysql_fetch_array($data, MYSQL_NUM)) { echo "<td>".$info['row 1']."</th>"; echo "<td>".$info['info for row 1']."</th>"; echo "<td>".$info['name for row 1']."</th>"; echo "</tr>"; echo "<tr>"; echo "<td>".$info['row 2']."</th>"; echo "<td>".$info['info for row 2']."</th>"; echo "<td>".$info['name for row 2']."</th>"; echo "</tr>"; echo "<tr>"; echo "<td>".$info['row 3']."</th>"; echo "<td>".$info['info for row 3']."</th>"; echo "<td>".$info['name for row 3']."</th>"; echo "</tr>"; } Quote Link to comment https://forums.phpfreaks.com/topic/196444-creating-a-table-via-database-records/#findComment-1031462 Share on other sites More sharing options...
-Karl- Posted March 25, 2010 Author Share Posted March 25, 2010 That would be too much hassle updating it whenever a new record is created. I've just tried it and it prints out each thing 3 times. EDIT echo "<tr>"; while($info = mysql_fetch_array($data, MYSQL_NUM)) { echo "<td>$info[0]</td>"; echo "<td>$info[1]</td>"; echo "<td>$info[2]</td>"; } echo "</tr>"; That works fine. I just need a way for it to end the <tr> after evert 3 it's outputted, and start a new one. Quote Link to comment https://forums.phpfreaks.com/topic/196444-creating-a-table-via-database-records/#findComment-1031464 Share on other sites More sharing options...
xcandiottix Posted March 25, 2010 Share Posted March 25, 2010 yeah you're right, duh.. i just realized the problem. Can you $dataset1 $dataset2 $dataset3 while($info = mysql_fetch_array($data, MYSQL_NUM)) { if (!(isset($_GET['dataset1']))){ $dataSet1 = $info[A] . $info[b] . $info[C] } if (!(isset($_GET['dataset2']))){ $dataSet2 = $info[A] . $info[b] . $info[C] } if (!(isset($_GET['dataset1']))){ $dataSet3 = $info[A] . $info[b] . $info[C] } } So this way it goes for each row in your table IF dataset1 is not set then set it to that row... after that dataset1 is set to something so the next row will go to data set number 2. Then in your table, you echo the var $dataset# Quote Link to comment https://forums.phpfreaks.com/topic/196444-creating-a-table-via-database-records/#findComment-1031467 Share on other sites More sharing options...
xcandiottix Posted March 25, 2010 Share Posted March 25, 2010 echo "<tr>"; while($info = mysql_fetch_array($data, MYSQL_NUM)) { echo "<td>$info[0]</td>"; echo "<td>$info[1]</td>"; echo "<td>$info[2]</td>"; } echo "</tr>"; If this is working then why not: echo "<tr>"; while($info = mysql_fetch_array($data, MYSQL_NUM)) { echo "<td>$info[0]</td>"; echo "<td>$info[1]</td>"; echo "<td>$info[2]</td>"; echo "</tr>"; echo "<tr>"; } echo "</tr>"; Quote Link to comment https://forums.phpfreaks.com/topic/196444-creating-a-table-via-database-records/#findComment-1031469 Share on other sites More sharing options...
-Karl- Posted March 25, 2010 Author Share Posted March 25, 2010 I couldn't get that to work either. It just prints everything 3 times. None of them work how I want this to work. I need to have __________________________________________ l row 1 l row 2 l row 2 l l info[0] for row 1 l info[0] for row 2 l info[0] for row 2 l l______________l______________l_____________l l row 3 l row 4 l row 5 l l info[0] for row 3 l info[0] for row 4 l info[0] for row 5 l l______________l______________l_____________l The printing of the data using: while($info = mysql_fetch_array($data, MYSQL_NUM)) { echo "<td>$info[0]</td>"; echo "<td>$info[1]</td>"; echo "<td>$info[2]</td>"; } Is working fine. I need a way to get the $i++; to work so that every 3 times it will echo </tr> <tr>, but it just won't work for me. That's the simplest solution I can think of. Quote Link to comment https://forums.phpfreaks.com/topic/196444-creating-a-table-via-database-records/#findComment-1031470 Share on other sites More sharing options...
xcandiottix Posted March 25, 2010 Share Posted March 25, 2010 $i=0 while($info = mysql_fetch_array($data, MYSQL_NUM)) { echo "<td>$info[0]</td>"; echo "<td>$info[1]</td>"; echo "<td>$info[2]</td>"; $i += 1 if ($i == 3){ echo '</tr><tr>' $i = 0 } } Quote Link to comment https://forums.phpfreaks.com/topic/196444-creating-a-table-via-database-records/#findComment-1031475 Share on other sites More sharing options...
-Karl- Posted March 25, 2010 Author Share Posted March 25, 2010 Parse error: syntax error, unexpected T_VARIABLE, expecting ',' or ';' on this line $i = 0; inside if ($i == 3){ echo '</tr><tr>' $i = 0; } Quote Link to comment https://forums.phpfreaks.com/topic/196444-creating-a-table-via-database-records/#findComment-1031476 Share on other sites More sharing options...
oni-kun Posted March 25, 2010 Share Posted March 25, 2010 Parse error: syntax error, unexpected T_IF $i += 1 if ($i == 3){ echo '</tr><tr>' $i = 0 --> $i++; if ($i == 3){ echo '</tr><tr>'; $i = 0; Quote Link to comment https://forums.phpfreaks.com/topic/196444-creating-a-table-via-database-records/#findComment-1031478 Share on other sites More sharing options...
-Karl- Posted March 25, 2010 Author Share Posted March 25, 2010 It worked, thank god for that. Thank you for all your help I really appreciate it. You're a life safer Quote Link to comment https://forums.phpfreaks.com/topic/196444-creating-a-table-via-database-records/#findComment-1031479 Share on other sites More sharing options...
xcandiottix Posted March 25, 2010 Share Posted March 25, 2010 sorry for the bad grammar.... and speaking flash +=1 glad you have it running. Quote Link to comment https://forums.phpfreaks.com/topic/196444-creating-a-table-via-database-records/#findComment-1031481 Share on other sites More sharing options...
oni-kun Posted March 25, 2010 Share Posted March 25, 2010 sorry for the bad grammar.... and speaking flash +=1 glad you have it running. Actionscript is ECMA (like JS), var++ is Java/C. Quote Link to comment https://forums.phpfreaks.com/topic/196444-creating-a-table-via-database-records/#findComment-1031483 Share on other sites More sharing options...
xcandiottix Posted March 25, 2010 Share Posted March 25, 2010 would $i+1 have worked then in this case? .. obviously, i++ would be the best choice.. just wondering. Quote Link to comment https://forums.phpfreaks.com/topic/196444-creating-a-table-via-database-records/#findComment-1031484 Share on other sites More sharing options...
oni-kun Posted March 25, 2010 Share Posted March 25, 2010 would $i+1 have worked then in this case? .. obviously, i++ would be the best choice.. just wondering. $i+=1 would have (double assigning), but $i++ is simply an iteration pointer (Post-increment as in other languages) such as ++$i for pre-incrementing. It's a faster pointer. Quote Link to comment https://forums.phpfreaks.com/topic/196444-creating-a-table-via-database-records/#findComment-1031487 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.