Jump to content

[SOLVED] Array returned from mysql_fetch_array...


triphis

Recommended Posts

Hello everyone :)

 

I am trying to figure out how to access the array returned from this function using the incremental approach ($i)... however $array[column][$i] doesn't seem to work... it just returns the first letter of that column's entry.

 

I can't use the standard while($row = mysql_fetch_array($result)) for what I am doing: say that loop runs 6 times. I am filling a table with 10 cells always - pulling the remaining cells from a completely different query. Right now, my table is being constructed with only 6 cells :( SO I want to have a FOR loop, and then use that $i to get what I need from both queries.

 

Anyway, all I am looking for is a description of the array returned, and how to access each column, and each row using increments.

 

Thanks!

 

Link to comment
Share on other sites

Of course, I apologize for giving barely any info lol.

 

Array ( [0] => 1 [id] => 1 [1] => jeantest [productcode] => jeantest [2] => True Religion [brand] => True Religion [3] => Micky Big T [style] => Micky Big T [4] => Denim [category] => Denim [5] => Wild Bill [colour] => Wild Bill [6] => 200 [cost] => 200 [7] => 300 [retail] => 300 [8] => 250 [sale] => 250 [9] => 0 [green] => 0 )

Link to comment
Share on other sites

I guess I am having an understanding-block. I thought I could just ask one question and magically I would figure it all out haha. However, I now see that I need more help, so I will share my entire problem.

 

Okay, so I am having a loop build the following table:

 

table.gif

 

The cyan cells are filled with content from the main loop, while the red cell is the "Feature Item" and pulled from a different query.

 

The code for the table loop is:

 

function showGrid($feature)
{

$featureresult = @mysql_query("SELECT * FROM matrix WHERE productcode = '$feature' LIMIT 1");
$featureItem = mysql_fetch_array($featureresult);

$otherresult = @mysql_query("SELECT * FROM matrix WHERE productcode != '$feature' ORDER BY rand()");
$otherItems = mysql_fetch_array($otherresult);

?>
<center>
<table border="1">
<tr>
<?php
$upper=1;
$int = 0;
while($otherItems = mysql_fetch_array($otherresult))  /////// this is where the problem is!!!
{
if($upper==1)
{
	if($int==2)
	{
		echo "<td rowspan='2' colspan='2' width='250' height='304' align='center'>" . $featureItem[brand] ."</td></tr><tr>";
		$int++;
	}
	elseif($int==4)
	{
		echo "<td width='125' height='152' align='center'>" . $otherItems[brand] ."</td></tr>";
		$upper=0;
		$int++;
	}
	else
	{
		echo "<td width='125' height='152' align='center'>" . $otherItems[brand] ."</td>";
		$int++;
	}
}
else
{
	if($int%4 == 1)
	{
		echo "<tr>";
	}

	echo "<td width='125' height='152' align='center'>" . $otherItems[brand] ."</td>";

	if($int%4 == 0)
	{
		echo "</tr>";
	}
}
}?>
</tr>
</table>
</center>
<?php
} //end function SHOWGRID

 

Okay, so as you can see above, the table loop is BASED off the results from the OTHER (non-feature) items. However, if there are only 3 items on this page, 2 non-feature, 1 feature... the table will only create 2 cells (because it will never reach the third iteration of the loop).

 

I would LIKE to convert that while statement to

for($i=0; $i < $num; $i++)

 

where $num = mysql_num_rows($otherresult) + 1; (to include the feature).

 

So that is why I wanted to use the incremental approach for my array...

 

Any better ideas?

 

Thanks!

Link to comment
Share on other sites

i still think i am missing something!!

<?php //<---Added*********************
//****************
function showGrid($feature)
{

$featureresult = @mysql_query("SELECT * FROM matrix WHERE productcode = '$feature' LIMIT 1");
$featureItem = mysql_fetch_array($featureresult);

$otherresult = @mysql_query("SELECT * FROM matrix WHERE productcode != '$feature' ORDER BY rand()");
$otherItems = mysql_fetch_array($otherresult);

?>
<center>
<table border="1">
<tr>
<?php
$upper=1;
$int = 0;

//CHANGED BELOW*********************
$num = mysql_num_rows($otherresult) + 1; //(to include the feature).
for($i=0; $i < $num; $i++)
//while($otherItems = mysql_fetch_array($otherresult))  /////// this is where the problem is!!!
//CHANGED ABOVE*********************
{
$otherItems = mysql_fetch_array($otherresult);
if($upper==1)
{
	if($int==2)
	{
		echo "<td rowspan='2' colspan='2' width='250' height='304' align='center'>" . $featureItem[brand] ."</td></tr><tr>";
		$int++;
	}
	elseif($int==4)
	{
		echo "<td width='125' height='152' align='center'>" . $otherItems[brand] ."</td></tr>";
		$upper=0;
		$int++;
	}
	else
	{
		echo "<td width='125' height='152' align='center'>" . $otherItems[brand] ."</td>";
		$int++;
	}
}
else
{
	if($int%4 == 1)
	{
		echo "<tr>";
	}

	echo "<td width='125' height='152' align='center'>" . $otherItems[brand] ."</td>";

	if($int%4 == 0)
	{
		echo "</tr>";
	}
}
}?>
</tr>
</table>
</center>
<?php
} //end function SHOWGRID

Link to comment
Share on other sites

That works just perfectly thank you!

 

I guess what I learned here today is that everytime $otherItems = mysql_fetch_array($otherresult);  is called, it advances to the next row, is that correct?

 

I was thinking that no matter how many times you call that, it would fetch the exact same array that the original result came up with :)

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.