Jump to content

[SOLVED] First Half of $result <split> Second Half of $result


Yves

Recommended Posts

$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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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. (:

Link to comment
Share on other sites

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.  :D

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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;
}
}

Link to comment
Share on other sites

<?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.

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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;
}
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

I see what you mean, and managed to make it work. 8)

 

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. :)

 

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

Very nice, rarebit! :D

 

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>";
	}
}
}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.