eugene2009 Posted November 19, 2009 Share Posted November 19, 2009 Hello Heres my code: $query = "SELECT * FROM comments ORDER BY date DESC"; //get the customer list $result = mysql_query($query); //perform the query //display the customer list while($row = mysql_fetch_array($result)){ echo '<a href="test1.php?id='.$row['commentid'].'">'.$row['name'].'</a><BR>'; //create a link with the customers id as the $_GET['id'] } What if I want 2 names per line.. how do i make it fetch all the rows displaying them 2 per line still in descending order? Thank you for all your help. Quote Link to comment https://forums.phpfreaks.com/topic/182104-fetching-rows-and-columns/ Share on other sites More sharing options...
emopoops Posted November 19, 2009 Share Posted November 19, 2009 set a variable to equal 0 then add to the variable for each while loop. then if its even number add the break<br> i dont know how to check if its even or odd i think there must be some function.... $variable = 0; then do while statemnt{ $variable = $variable++; if(Num % 2 == 0) { // It's even so you can echo "<br>"; } else { // It's odd so do two per line(no line break) } }end while Quote Link to comment https://forums.phpfreaks.com/topic/182104-fetching-rows-and-columns/#findComment-960667 Share on other sites More sharing options...
mikesta707 Posted November 19, 2009 Share Posted November 19, 2009 if you want to check for even and odd, you can do if ($number % 2 == 0) { //even } else {} //odd you could stick it in a function if you want, and just call it <?php function isEven($number){ return (bool)($number % 2) == 0; } echo var_dump(isEven(4))."<br /.>"; echo var_dump(isEven(5)); ?> output: bool(true) bool(false) The % is called modulus. It basically divides the two numbers (integer division) and returns the whole number remainder. For example, 15 % 6 would be 3, because the closest multiple of 6 is 12, and 15-12 (which is the remainder) is 3. Quote Link to comment https://forums.phpfreaks.com/topic/182104-fetching-rows-and-columns/#findComment-960670 Share on other sites More sharing options...
emopoops Posted November 19, 2009 Share Posted November 19, 2009 yeah. i think its solved. pretty simple. Quote Link to comment https://forums.phpfreaks.com/topic/182104-fetching-rows-and-columns/#findComment-960677 Share on other sites More sharing options...
eugene2009 Posted November 19, 2009 Author Share Posted November 19, 2009 is it possible to do it like this??? 1 6 2 7 3 8 4 9 5 10 instead of 1 2 3 4 5 6 etc... Quote Link to comment https://forums.phpfreaks.com/topic/182104-fetching-rows-and-columns/#findComment-960700 Share on other sites More sharing options...
emopoops Posted November 19, 2009 Share Posted November 19, 2009 you could put two tables next to each other with one result per line. when u get to $variable = 6 then end the table and start the new table. there would need to be some css to make it apear side by side. is there a lot of results? Quote Link to comment https://forums.phpfreaks.com/topic/182104-fetching-rows-and-columns/#findComment-960718 Share on other sites More sharing options...
eugene2009 Posted November 19, 2009 Author Share Posted November 19, 2009 well heres the thing.. that was just an example code to make things easier.. I have an upload form where users upload pictures into the database and this page is the one that displays them you know? theres 10 results per page.. i have it paginated.. but i want it to display 2 pictures per row and then go to the next.. how is this possible.. Quote Link to comment https://forums.phpfreaks.com/topic/182104-fetching-rows-and-columns/#findComment-960730 Share on other sites More sharing options...
mikesta707 Posted November 19, 2009 Share Posted November 19, 2009 this should give you an example of how to do it $array = array(); for ($i = 0; $i < 18; $i++){ $array[] = $i; } $midPoint = ceil(count($array) / 2); echo $midPoint; echo "<table>"; for($i = 0; $i < $midPoint; $i++){ echo "<tr>"; echo "<td>{$array[$i]}</td>"; echo "<td>"; if (isset($array[$i + $midPoint])) echo $array[$i+$midPoint]; echo "</td>"; echo "</tr>"; } seems to work for even and odd numbers, but I haven't throughly tested it. It puts them in 2 columns only, won't do 3 or more. What you would need to do is store the information you wanted from your query into an array, something like $array = array() while($row = mysql_fetch_xxx()){ $array[] = $row['name'];//if I just wanted the name //or $array[] = $row;//if I want the whole row If you are just getting a single column, that could should work out of the box (just replace the array in the code with yours) If you are getting the multidimensional array, you would need to change it to something like //assume array is multidimensional $midPoint = ceil(count($array) / 2); echo $midPoint; echo "<table>"; for($i = 0; $i < $midPoint; $i++){ echo "<tr>"; echo "<td>".$array[$i]['name']."</td>"; echo "<td>"; if (isset($array[$i + $midPoint])) echo $array[$i+$midPoint]['name']; echo "</td>"; echo "</tr>"; } Quote Link to comment https://forums.phpfreaks.com/topic/182104-fetching-rows-and-columns/#findComment-960734 Share on other sites More sharing options...
emopoops Posted November 19, 2009 Share Posted November 19, 2009 if the number variable ends in 5 (or divided by 5 evenly and isnt a multiple of ten) then end the table and start the second table for the right (set of 5 image) ? maybe set two tables of equal width inside one table of total width. like the first table would be made before the loop and it would be width: 600 then the table with one colum and 5 rows would be width:300 and then the next table would be on the right of that table would be width: 300 so they would both fit inside the 600. dont know exactly maybe the two tables have to be like 298 or something because of borders or something. that should world. then on 5, end the left table and start the right <table width="600"> <tr><td> <table width="300"> <?php $number = "0"; while($row = mysql_fetch_array($result)){$number = $number++; echo '<tr><td><a href="test1.php?id='.$row['commentid'].'">'.$row['name'].'</a></td></tr>'; //create a link with the customers id as the $_GET['id'] if($number == "5"){echo "</table><table width='300'>";} } ?> </table> </td></tr></table> Quote Link to comment https://forums.phpfreaks.com/topic/182104-fetching-rows-and-columns/#findComment-960736 Share on other sites More sharing options...
eugene2009 Posted November 19, 2009 Author Share Posted November 19, 2009 Sorry, I am very new to this.. i am having trouble on how to replace my array with this one.. may you please help? heres the code that is working right now.. $query = "SELECT * FROM comments ORDER BY date DESC"; //get the customer list $result = mysql_query($query); //perform the query //display the customer list while($row = mysql_fetch_array($result)){ echo '<a href="test1.php?id='.$row['commentid'].'">'.$row['name'].'</a><BR>'; //create a link with the customers id as the $_GET['id'] } Thank you so much for your help. Quote Link to comment https://forums.phpfreaks.com/topic/182104-fetching-rows-and-columns/#findComment-960738 Share on other sites More sharing options...
mikesta707 Posted November 19, 2009 Share Posted November 19, 2009 $query = "SELECT * FROM comments ORDER BY date DESC"; //get the customer list $result = mysql_query($query); //perform the query //display the customer list $array = array() while($row = mysql_fetch_array($result)){ $array[] = $row; } $midPoint = ceil(count($array) / 2); echo $midPoint; echo "<table>"; for($i = 0; $i < $midPoint; $i++){ echo "<tr>"; echo '<td><a href="test1.php?id='.$array[$i]['commentid'].'">'.$array[$i]['name'].'</a></td'; echo "<td>"; if (isset($array[$i + $midPoint])) echo '<td><a href="test1.php?id='.$array[$i+$midPoint]['commentid'].'">'.$array[$i+$midPoint]['name'].'</a></td'; echo "</td>"; echo "</tr>"; } untested, but should be about right Quote Link to comment https://forums.phpfreaks.com/topic/182104-fetching-rows-and-columns/#findComment-960743 Share on other sites More sharing options...
emopoops Posted November 19, 2009 Share Posted November 19, 2009 the array makes no sense to do. its an unneeded complication Quote Link to comment https://forums.phpfreaks.com/topic/182104-fetching-rows-and-columns/#findComment-960746 Share on other sites More sharing options...
eugene2009 Posted November 19, 2009 Author Share Posted November 19, 2009 yea.. it doesnt work.. hmmm.. emopoops.. how would you plug yours in? sorry its just that i learn when i ask for help. Quote Link to comment https://forums.phpfreaks.com/topic/182104-fetching-rows-and-columns/#findComment-960756 Share on other sites More sharing options...
emopoops Posted November 19, 2009 Share Posted November 19, 2009 i gave u the exact code LOL Quote Link to comment https://forums.phpfreaks.com/topic/182104-fetching-rows-and-columns/#findComment-960760 Share on other sites More sharing options...
mikesta707 Posted November 19, 2009 Share Posted November 19, 2009 this code worked for me... $query = "SELECT * FROM users"; //get the customer list $result = mysql_query($query); //perform the query //display the customer list $array = array(); while($row = mysql_fetch_array($result)){ $array[] = $row; } $midPoint = ceil(count($array) / 2); echo "<table border=\"1\">"; for($i = 0; $i < $midPoint; $i++){ echo "<tr>"; echo '<td><a href="test1.php?id='.$array[$i]['username'].'">'.$array[$i]['username'].'</a></td'; echo "<td>"; if (isset($array[$i + $midPoint])) echo '<td><a href="test1.php?id='.$array[$i+$midPoint]['username'].'">'.$array[$i+$midPoint]['username'].'</a></td'; echo "</td>"; echo "</tr>"; } it changed the columns to username, and it showed me a 2 column table.. Quote Link to comment https://forums.phpfreaks.com/topic/182104-fetching-rows-and-columns/#findComment-960763 Share on other sites More sharing options...
emopoops Posted November 19, 2009 Share Posted November 19, 2009 ok whatever making a two column table is easy the op asked for a certain way like 1-5 on the left then 6-10 on the right Quote Link to comment https://forums.phpfreaks.com/topic/182104-fetching-rows-and-columns/#findComment-960779 Share on other sites More sharing options...
eugene2009 Posted November 19, 2009 Author Share Posted November 19, 2009 Awesome! it worked! How would I be able to paginate this so it will display 3 rows per page? Thank you Quote Link to comment https://forums.phpfreaks.com/topic/182104-fetching-rows-and-columns/#findComment-960786 Share on other sites More sharing options...
emopoops Posted November 19, 2009 Share Posted November 19, 2009 three rows is 6 things right? u have to set a limit on your mysql query to get 6 things at a time. this is the pagenator that works the best for me: if (isset($_GET )) {$page = mysql_real_escape_string($_GET ); if($page <= 0) {$page = 1;}} else {$page = 1;} $limit = 6;<---that thing is the amount of things per page $numpages = ceil($totalnoapps / $limit); $start = $page * $limit; $realstart = $start - $limit; if($page != 1) {$prevs = $page - 1; $startpage = $page;} else {$startpage = 1;} for ($pix = $startpage; $pix <= $page+6; $pix++) {if ($pix <= $numpages && $totalnoapps > $limit) {if($pix == $page) { ?> <a href="yourpage.php?page=<?php echo $prevs; ?>"><?php echo $prevs; ?></a> <span style="color:white;font-family:arial narrow;font-size:55px; line-height:55pt;letter-spacing:-3px"><?php echo $pix; ?></span> <?php } else{ ?> <a href="yourpage.php?page=<?php echo $pix; ?>"><?php echo $pix; ?></a> <?php }}} $get = mysql_query("SELECT * FROM blah WHERE `whatever urusing` = '$whatever ur doing' ORDER BY `id` DESC LIMIT $realstart, $limit"); then the while statment goes here. Quote Link to comment https://forums.phpfreaks.com/topic/182104-fetching-rows-and-columns/#findComment-960794 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.