Yves Posted September 20, 2007 Share Posted September 20, 2007 $sql = "SELECT * FROM table WHERE var = ".$string.""; $result = $obj_db->select($sql); Assume $result is 21 rows in total and I want to display the first half of those rows in a left column and the second half of those rows in a right colomn. $result show be spilt in two. something like this...: $sql = "SELECT * FROM table WHERE var = ".$string.""; if($pos=="left") { $result = *Let-Me-Be-The-First-Half-Of*($sql); } if($pos=="right") { $result = *Let-Me-Be-The-Second-Half-Of*($sql); } How can I make $result be 11 the first rows when $pos is set to 'left'? How can I make $result be 10 the last rows when $pos is set to 'right'? I hope I explained what I want clear enough. Quote Link to comment https://forums.phpfreaks.com/topic/70011-solved-first-half-of-result-second-half-of-result/ Share on other sites More sharing options...
Orio Posted September 20, 2007 Share Posted September 20, 2007 You could do something like this: <?php echo "<table><tr>"; $all = mysql_num_rows($result); $half = floor($all / 2); echo "<td width=\"50%\">"; for($i=0; $i < $half; $i++) { $row=mysql_fetch_array($result); //print what you want } echo "</td><td width=\"50%\">"; while($row=mysql_fetch_array($result) { $row=mysql_fetch_array($result); //print what you want2 } ?> Orio. Quote Link to comment https://forums.phpfreaks.com/topic/70011-solved-first-half-of-result-second-half-of-result/#findComment-351623 Share on other sites More sharing options...
NArc0t1c Posted September 20, 2007 Share Posted September 20, 2007 Ahh, Orio beat me to it, here is mine anyway. Well so you want the array in two halves? Try this. <?php $sql = "SELECT * FROM table WHERE var = ".$string.""; $result = mysql_fetch_assoc($obj_db->select($sql)); $number = 0; $first = array(); $end = array(); foreach ($result as $r) { while ($number <= (count($result)/2)) { $number++; $first[] = $r; } while($number > (count($result)/)) { $number++; $end[] = $r; } } ?> I don't know if it will work, but theory is there. (: Quote Link to comment https://forums.phpfreaks.com/topic/70011-solved-first-half-of-result-second-half-of-result/#findComment-351625 Share on other sites More sharing options...
Yves Posted September 20, 2007 Author Share Posted September 20, 2007 I'm actually writing a function. If I would try methods suggested above I would end up writing double as much code. However, this is what I tried... <tr> <td><?php echo Display($obj_db,0,left); ?></td> <td><?php echo Display($obj_db,0,right); ?></td> </tr> function Display($obj_db="",$ParentID,$pos) { $sql = "SELECT * FROM tblcategories WHERE intParentID = ".$ParentID." ORDER BY varCategory ASC"; $result = $obj_db->select($sql); $all = mysql_num_rows($result); $half = floor($all / 2); if($pos == "left") { for($i=0; $i <= $half; $i++) { $row = mysql_fetch_array($result); } } if($pos == "right") { for($i=0; $i > $half; $i++) { $row = mysql_fetch_array($result); } } for($i=0; $i < $all; $i++) { $maincat = $row[$i]['varCategory']; // everything I need I can write once, right here. // in the end it will display different data in both left and right column echo $maincat; } } This piece of code is incorrect though. And I still lack the knowledge to spot why it is. I bet someone here spots it instantly. Quote Link to comment https://forums.phpfreaks.com/topic/70011-solved-first-half-of-result-second-half-of-result/#findComment-351858 Share on other sites More sharing options...
Yves Posted September 22, 2007 Author Share Posted September 22, 2007 I still need some help. Quote Link to comment https://forums.phpfreaks.com/topic/70011-solved-first-half-of-result-second-half-of-result/#findComment-353028 Share on other sites More sharing options...
rarebit Posted September 22, 2007 Share Posted September 22, 2007 Here's the phat hip way! function halves($sql) { $result = $obj_db->select($sql); $all = mysql_num_rows($result); $l = "<td>"; $r = "<td>"; for($i=0;$i<$all;$i++) { $flag = (($i % 2) == 0) ? 'l' : 'r'; if($flag == 'l') { $l .= mysql_fetch_array($result); } else { $r .= mysql_fetch_array($result); } } $l .= "</td>"; $r .= "</td>"; return "<tr>".$l.$r."</tr>"; } echo halves("SELECT * FROM tblcategories WHERE intParentID = ".$ParentID." ORDER BY varCategory ASC"); I've not checked it but, hey ho! Quote Link to comment https://forums.phpfreaks.com/topic/70011-solved-first-half-of-result-second-half-of-result/#findComment-353032 Share on other sites More sharing options...
rarebit Posted September 22, 2007 Share Posted September 22, 2007 Sorry, I don't know how to read?? function Display($obj_db="",$ParentID,$pos) { $sql = "SELECT * FROM tblcategories WHERE intParentID = ".$ParentID." ORDER BY varCategory ASC"; $result = $obj_db->select($sql); $all = mysql_num_rows($result); $half = floor($all / 2); $start = 0; if($pos == "left") { $start = $half; } for($i=$start; $i <= $half; $i++) { $row = mysql_fetch_array($result); } for($i=0; $i < $all; $i++) { $maincat = $row[$i]['varCategory']; // everything I need I can write once, right here. // in the end it will display different data in both left and right column echo $maincat; } } Quote Link to comment https://forums.phpfreaks.com/topic/70011-solved-first-half-of-result-second-half-of-result/#findComment-353035 Share on other sites More sharing options...
MmmVomit Posted September 22, 2007 Share Posted September 22, 2007 <?php $sql = "SELECT field FROM table;"; // use your sql query here $result = mysql_query($sql); $rows = Array(); $num_rows = mysql_num_rows($result); while($rows[] = mysql_fetch_assoc($result)){;} // fetch all rows into an array ?> <table> <?php for($i = 0; i < ($num_rows / 2) + ($num_rows % 2); $i++) { ?> <tr> <td><?=$rows[$i]['field']?></td> <td><?=(isset($rows[2 * $i]) ? $rows[2 * $i]['field'] : " ")?></td> </tr> <?php } ?> </table> Something like that should do it. It should be easy enough to turn into a function. I don't have PHP on this computer, so I apologize for any bugs. Quote Link to comment https://forums.phpfreaks.com/topic/70011-solved-first-half-of-result-second-half-of-result/#findComment-353040 Share on other sites More sharing options...
Yves Posted September 24, 2007 Author Share Posted September 24, 2007 rarebit I tried your code, but it didn't work. Changed it to this: function Display($obj_db="",$ParentID,$pos) { $sql = "SELECT * FROM tblcategories WHERE intParentID = ".$ParentID." ORDER BY varCategory ASC"; $result = $obj_db->select($sql); $all = count($result); $half = floor($all / 2); if($pos == "left") { $start = 0; $end = $half; } if($pos == "right") { $start = $half; $end = $all; } for($i=$start; $i <= $end; $i++) { $row = mysql_fetch_array($result); } for($i=0; $i < $all; $i++) { $maincat = $row[$i]['varCategory']; // everything I need I can write once, right here. // in the end it will display different data in both left and right column echo $maincat; } } but I get a Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in config.inc.php on line 256 Quote Link to comment https://forums.phpfreaks.com/topic/70011-solved-first-half-of-result-second-half-of-result/#findComment-353898 Share on other sites More sharing options...
rarebit Posted September 24, 2007 Share Posted September 24, 2007 Not checked it, but this looks better: function Display($obj_db="",$ParentID,$pos) { $sql = "SELECT * FROM tblcategories WHERE intParentID = ".$ParentID." ORDER BY varCategory ASC"; $result = $obj_db->select($sql); //$all = count($result); $all = mysql_num_rows($result); $half = floor($all / 2); if($pos == "left") { $start = 0; $end = $half; } if($pos == "right") { $start = $half; $end = $all; } for($i=$start; $i <= $end; $i++) { //$row = mysql_fetch_array($result); $row[] = mysql_fetch_array($result); } for($i=0; $i < $all; $i++) { $maincat = $row[$i]['varCategory']; // everything I need I can write once, right here. // in the end it will display different data in both left and right column echo $maincat; } } Quote Link to comment https://forums.phpfreaks.com/topic/70011-solved-first-half-of-result-second-half-of-result/#findComment-353909 Share on other sites More sharing options...
Yves Posted September 24, 2007 Author Share Posted September 24, 2007 I can see why it would work. Though, instead, it gave us an extra warning. Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in config.inc.php on line 249 Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in config.inc.php on line 256 Quote Link to comment https://forums.phpfreaks.com/topic/70011-solved-first-half-of-result-second-half-of-result/#findComment-354032 Share on other sites More sharing options...
rarebit Posted September 24, 2007 Share Posted September 24, 2007 I don't use a db object like you, so my query statement is like this: $result = mysql_query($sql, $conn) your doing a query with this: $result = $obj_db->select($sql); which actually looks more like your selecting a database??? Do you see what I mean, otherwise show more of your database code... Quote Link to comment https://forums.phpfreaks.com/topic/70011-solved-first-half-of-result-second-half-of-result/#findComment-354166 Share on other sites More sharing options...
Yves Posted September 24, 2007 Author Share Posted September 24, 2007 I see what you mean, and managed to make it work. Though, the right column shows exactly the same content as the left column. I'll be a little bit more specific. My $result counts 27 rows (14&13), default. I edited the function to shows 14 results in the left column, and 13 in the right. 14: row 0 -> row 13 13: row 14 -> row 26 Notice the changes at if($pos == "right"): <?php function Display($obj_db="",$ParentID,$pos) { $sql = "SELECT * FROM tblcategories WHERE intParentID = ".$ParentID." ORDER BY varCategory ASC"; $result = mysql_query($sql); $all = mysql_num_rows($result); $half = floor($all / 2); if($pos == "left") { $start = 0; $end = $half; } if($pos == "right") { $start = $half+1; $end = $all-1; } for($i=$start; $i <= $end; $i++) { $row[] = mysql_fetch_array($result); } for($i=0; $i < $all; $i++) { $maincat = $row[$i]['varCategory']; // everything I need I can write once, right here. // in the end it will display different data in both left and right column echo $maincat; echo "<br>"; } } ?> The only thing it didn't do is change the content of the row[] array. Both left and right column start with the same stuff. Quote Link to comment https://forums.phpfreaks.com/topic/70011-solved-first-half-of-result-second-half-of-result/#findComment-354234 Share on other sites More sharing options...
rarebit Posted September 24, 2007 Share Posted September 24, 2007 I'm not keen, but: function Display($obj_db="",$ParentID,$pos) { $sql = "SELECT * FROM tblcategories WHERE intParentID = ".$ParentID." ORDER BY varCategory ASC"; $result = mysql_query($sql); $all = mysql_num_rows($result); $half = floor($all / 2); if($pos == "left") { $start = 0; $end = $half; } if($pos == "right") { $start = $half+1; $end = $all-1; } for($i=0; $i <= $all; $i++) { if(($i >= $start )&& ($i <= $end)) { //$row[] = mysql_fetch_array($result); $row = mysql_fetch_array($result); echo $row['varCategory']; } } } Are you sure that query works? Quote Link to comment https://forums.phpfreaks.com/topic/70011-solved-first-half-of-result-second-half-of-result/#findComment-354289 Share on other sites More sharing options...
Yves Posted September 24, 2007 Author Share Posted September 24, 2007 Yes, that one works exactly the same. But both left and right show same results. (appart from left column having 1 item more, as should) here's a picture: Quote Link to comment https://forums.phpfreaks.com/topic/70011-solved-first-half-of-result-second-half-of-result/#findComment-354292 Share on other sites More sharing options...
rarebit Posted September 24, 2007 Share Posted September 24, 2007 lollipop! for($i=0; $i <= $all; $i++) { $row = mysql_fetch_array($result); if(($i >= $start )&& ($i <= $end)) { echo $row['varCategory']; } } Quote Link to comment https://forums.phpfreaks.com/topic/70011-solved-first-half-of-result-second-half-of-result/#findComment-354310 Share on other sites More sharing options...
rarebit Posted September 24, 2007 Share Posted September 24, 2007 P.S. I'm not keen because it's called twice, therefore making two mysql calls, and wastes iterations through the array and fetch_array... argh!!!! Quote Link to comment https://forums.phpfreaks.com/topic/70011-solved-first-half-of-result-second-half-of-result/#findComment-354311 Share on other sites More sharing options...
Yves Posted September 24, 2007 Author Share Posted September 24, 2007 Very nice, rarebit! thanks a bunch. Here's what I think is completely right: <?php function Display($obj_db="",$ParentID,$pos) { $sql = "SELECT * FROM tblcategories WHERE intParentID = ".$ParentID." ORDER BY varCategory ASC"; $result = mysql_query($sql); $all = mysql_num_rows($result); $half = floor($all / 2); if($pos == "left") { // $start $start = 0; // $end if ($all % 2 == 1) { // when $all is uneven $end = $half; } if ($all % 2 == 0) { // when $all is even $end = $half-1; } } if($pos == "right") { // $start if ($all % 2 == 1) { // when $all is uneven $start = $half+1; } if ($all % 2 == 0) { // when $all is even $start = $half; } // $end $end = $all-1; } for($i=0; $i <= $all; $i++) { $row = mysql_fetch_array($result); if(($i >= $start )&& ($i <= $end)) { echo $row['varCategory']; echo "<br>"; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/70011-solved-first-half-of-result-second-half-of-result/#findComment-354332 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.