Jump to content

mysqli_fetch_array($result, MYSQLI_ASSOC) while loop problem


kasitzboym

Recommended Posts

Hey everyone I've been working on converting my old mysql code to to the mysqli version and am running into some trouble. I am pulling posts form phpbb and displaying them in a script. My first script below works just fine.

 

<?php
require("connect.php");
$query="SELECT * FROM phpbb_topics WHERE (forum_id='13' OR forum_id='14') AND topic_status = '0'";
$query2="SELECT * FROM phpbb_topics WHERE (forum_id='15' OR forum_id='16') AND topic_status = '0'";
$result=mysql_query($query);
$result2=mysql_query($query2);

	echo "<table border=0 cellpadding=2>";
	echo "<tr><td>Lost</td><td>Found</td></tr>";
	while($row=(mysql_fetch_array($result)) || $row2=(mysql_fetch_array($result2)))
	{

		$forum_id=mysql_result($result,$k,"forum_id");
		$topic_id=mysql_result($result,$k,"topic_id");
		$topic_title=mysql_result($result,$k,"topic_title");

		$forum_id2=mysql_result($result2,$k,"forum_id");
		$topic_id2=mysql_result($result2,$k,"topic_id");
		$topic_title2=mysql_result($result2,$k,"topic_title");

		echo "<tr><td width='15%'><a href=/sahbb/viewtopic.php?f=$forum_id&t=$topic_id>$topic_title</a></td>
		<td width='15%'><a href=/sahbb/viewtopic.php?f=$forum_id2&t=$topic_id2>$topic_title2</a></td>
		</tr>";
		$k++;

	}
	echo "</table>";
	mysql_close($connect);
	?>

 

however its not very clean or memory efficient so i have converted it to the following.

 


<?php
require("includes_database/mysqli_connect.php");
$query="SELECT * FROM phpbb_topics WHERE (forum_id='13' OR forum_id='14')";
$query2="SELECT * FROM phpbb_topics WHERE (forum_id='15' OR forum_id='16')";
$result=mysqli_query($dbc, $query);
$result2=mysqli_query($dbc, $query2);

	echo "<table border=1 cellpadding=2>";
	echo "<tr><td>Lost</td><td>Found</td></tr>";
	while(($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) || ($row2 = mysqli_fetch_array($result2, MYSQLI_ASSOC)))
	{

		echo "<tr><td width='15%'><a href=/sahbb/viewtopic.php?f=".$row['forum_id']."&t=".$row['topic_id'].">".$row['topic_title']."</a></td>
		<td width='15%'><a href=/sahbb/viewtopic.php?f=".$row2['forum_id']."&t=".$row2['topic_id'].">".$row2['topic_title']."</a></td>
		</tr>";

	}
	echo "</table>";

	mysqli_free_result($result);
	mysqli_free_result($result2);
	mysqli_close($dbc);

	?>

 

I know the error is in my while loop but i cant seem to figure out exactly where. The new code displays all of the first query in the table and then below that it displays the next query insted of displaying them side by side below would be an example of what is happening

 

 

[table

Lost Found
Lost, Australian Sheppard

 

[/td]

 

Lost German Shepherded / Collie Mix

 

 

Lost Golden Retriever/Cocker Mix

 

 

Lost Female Grey Tiger

 

 

Lost Pitbull Mix

 

 

Lost 2 Chihuahua

 

 

Lost Black Lab

 

 

Lost - German Shepard Mix and Lab Mix

 

 

Lost - Female German Sheperd

 

 

 

Aprox 8 Week Old Kitten Found!

 

 

2 Pugs FOUND

 

 

Found 1 Corgi female/ 1 Shih Tzu male

 

 

Found Tan and Black Australian Shepard

 

 

Found Australian Shepard

 

 

[td]Found Intact Male Pitbull/Lab Mix

 

 

Any help that any of you could provide would be greatly appreciated!

Link to comment
Share on other sites

or try

	<?php
require("includes_database/mysqli_connect.php");
$query="SELECT * FROM phpbb_topics WHERE (forum_id='13' OR forum_id='14')";
$query2="SELECT * FROM phpbb_topics WHERE (forum_id='15' OR forum_id='16')";
$result=mysqli_query($dbc, $query);
$result2=mysqli_query($dbc, $query2);

	echo "<table border=1 cellpadding=2>";
	echo "<tr><td>Lost</td><td>Found</td></tr>";
                $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
                $row2 = mysqli_fetch_array($result2, MYSQLI_ASSOC);
	while($row || $row2)
	{
		echo "<tr>";
                        if($row) echo "<td width='15%'><a href=/sahbb/viewtopic.php?f=".$row['forum_id']."&t=".$row['topic_id'].">".$row['topic_title']."</a></td>\n" else echo "<td> </td>\n";
		if($row2) echo"<td width='15%'><a href=/sahbb/viewtopic.php?f=".$row2['forum_id']."&t=".$row2['topic_id'].">".$row2['topic_title']."</a></td>\n" else echo "<td> </td>\n";
		echo "</tr>\n";
		$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
                        $row2 = mysqli_fetch_array($result2, MYSQLI_ASSOC);
	}
	echo "</table>";

	mysqli_free_result($result);
	mysqli_free_result($result2);
	mysqli_close($dbc);
	?>

Link to comment
Share on other sites

or try

	<?php
require("includes_database/mysqli_connect.php");
$query="SELECT * FROM phpbb_topics WHERE (forum_id='13' OR forum_id='14')";
$query2="SELECT * FROM phpbb_topics WHERE (forum_id='15' OR forum_id='16')";
$result=mysqli_query($dbc, $query);
$result2=mysqli_query($dbc, $query2);

	echo "<table border=1 cellpadding=2>";
	echo "<tr><td>Lost</td><td>Found</td></tr>";
                $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
                $row2 = mysqli_fetch_array($result2, MYSQLI_ASSOC);
	while($row || $row2)
	{
		echo "<tr>";
                        if($row) echo "<td width='15%'><a href=/sahbb/viewtopic.php?f=".$row['forum_id']."&t=".$row['topic_id'].">".$row['topic_title']."</a></td>\n" else echo "<td> </td>\n";
		if($row2) echo"<td width='15%'><a href=/sahbb/viewtopic.php?f=".$row2['forum_id']."&t=".$row2['topic_id'].">".$row2['topic_title']."</a></td>\n" else echo "<td> </td>\n";
		echo "</tr>\n";
		$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
                        $row2 = mysqli_fetch_array($result2, MYSQLI_ASSOC);
	}
	echo "</table>";

	mysqli_free_result($result);
	mysqli_free_result($result2);
	mysqli_close($dbc);
	?>

 

This seems to have returned the same result as my version.

 

 

Your loop construct does first one, then the other, because you're using the logical OR.

 

If you want to do side-by-side tables, the easiest way is to have two sets of side-by-side DIVs with fixed heights floating next to each other.

 

ManiacDan

I will try this however i would like to keep a single table with two columns instead of splitting them up They are currently using the same div tag as they are both in a marque that scrolls.

Link to comment
Share on other sites

Change it to an && so both have to run concurrently. Then after the loop it's possible that one resultset is empty while the other is not, so you need another loop on whichever resultset isn't to fill in the rest of the table (making sure the other column gets additional empty cells).

Link to comment
Share on other sites

Yeah this is a pretty silly way of doing it, but requinix is right.  You need:

 

loop where both fetches are && together.

if row is false, loop through result2 and display the right-column

if row2 is false, loop through result and display the left-column

 

Still though...this is weird and difficult.

 

-Dan

Link to comment
Share on other sites

That kind of makes since. Although i am kind of lost on the syntax that you are explaining

do you mean someting like this?

 


<?php
require("includes_database/mysqli_connect.php");
//$query="SELECT * FROM phpbb_topics WHERE (forum_id='13' OR forum_id='14') AND topic_status = '0'";
//$query2="SELECT * FROM phpbb_topics WHERE (forum_id='15' OR forum_id='16') AND topic_status = '0'";
$query="SELECT * FROM phpbb_topics WHERE (forum_id='13' OR forum_id='14')";
$query2="SELECT * FROM phpbb_topics WHERE (forum_id='15' OR forum_id='16')";
$result=mysqli_query($dbc, $query);
$result2=mysqli_query($dbc, $query2);

	$num = mysqli_num_rows($result);
$num2 = mysqli_num_rows($result2);

echo''.count($num).'<br>';
echo''.count($num2).'<br>';

	echo "<table border=1 cellpadding=2>";
	echo "<tr><td>Lost</td><td>Found</td></tr>";
	while(($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) && ($row2 = mysqli_fetch_array($result2, MYSQLI_ASSOC)))
	{
		echo'<tr>';
		if ($row){
			while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
				echo "<td width='15%'><a href=/sahbb/viewtopic.php?f=".$row['forum_id']."&t=".$row['topic_id'].">".$row['topic_title']."</a></td>";
			}
		}
		if($row2){
			while($row2 = mysqli_fetch_array($result2, MYSQLI_ASSOC)){
				echo "<td width='15%'><a href=/sahbb/viewtopic.php?f=".$row2['forum_id']."&t=".$row2['topic_id'].">".$row2['topic_title']."</a></td>";
			}
		}
		echo'</tr>';

	}
	echo "</table>";

	mysqli_free_result($result);
	mysqli_free_result($result2);
	mysqli_close($dbc);

	?>

Link to comment
Share on other sites

Sorting them out into an array then putting that into the table makes it easier.  The code below is untested, but shows the basic idea.

 

require("includes_database/mysqli_connect.php");
$query="SELECT forum_id, topic_title, topic_id,  FROM phpbb_topics WHERE forum_id IN (13, 14, 15, 16)";
$result=mysqli_query($dbc, $query);

$whichColumn = array(13 => 0, 14 => 0, 15 => 1, 16 => 1);
$columns = array();
while ($row=mysqli_fetch_array($result, MYSQLI_ASSOC)){
$col = $whichColumn[$row['forum_id']];
$columns[$col][] = $row;
}

echo "<table border=1 cellpadding=2>";
echo "<tr><td>Lost</td><td>Found</td></tr>";
for ($i=0,$max=max(count($columns[0]), count($columns[1])); $i<$max; $i++)
{
echo'<tr>';
if (isset($columns[0][$i])){
	$row = $columns[0][$i];
	echo "<td width='15%'><a href=/sahbb/viewtopic.php?f=".$row['forum_id']."&t=".$row['topic_id'].">".$row['topic_title']."</a></td>";
}
else {
	echo "<td width='15%'> </td>";
}


if(isset($columns[1][$i])){
	$row = $columns[1][$i];
	echo "<td width='15%'><a href=/sahbb/viewtopic.php?f=".$row['forum_id']."&t=".$row['topic_id'].">".$row['topic_title']."</a></td>";
}
else {
	echo "<td width='15%'> </td>";
}
echo'</tr>';

}
echo "</table>";

mysqli_free_result($result);
mysqli_close($dbc);

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.