Jump to content

Array Problem


Hooch
 Share

Recommended Posts

Hello all.Here's my code

		for ($i=1; $i <= 8; $i++)		{			$artist   = mysql_query("SELECT `artist` FROM `names` WHERE `pos`='$i'");			$r		= mysql_fetch_array($artist);			$name[$i] = $r['artist']; 		}

I have 8 entries in this table consisting of 2 fields: "pos" and "artist"I'm trying to set variables:$name1 = the artist in pos 1$name2 = the artist in pos 2$name3 = the artist in pos 3$name4 = the artist in pos 4etc.Thanks for any help.

Link to comment
Share on other sites

Hi, in your code you have this line:$name[$i] = $r['artist'];this is going to work as an array not as independent variables, of course having:$name = array();before the 'for' block, also i don't think the $r['artist'] contains any value, maybe this one: $r[0]['artist'], in that case something like:$name[$i] = isset($r[0]['artist']) ? $r[0]['artist'] : '';well, this leaves only one variable instead of 8+, and avoid the warning in the case of empty values.hope this helps.

Link to comment
Share on other sites

This is a bad way for my to query anyway. This table may grow or shrink dynamically. So I should query then print out the array.But I do not know how to set each array to a variable.This should be my query...

<?PHP	$artist   = mysql_query("SELECT `artist` FROM `names` ORDER BY `pos`");	$r		= mysql_fetch_array($artist);

I tried printing...$r[0], $r[1], $r[2]...$r[7]But only $r[0] prints out.**EDIT**Getting closer...

	$artist = mysql_query("SELECT `artist` FROM `names` ORDER BY `pos` ASC");	while($array = mysql_fetch_array($artist))	{		print_r ($array);		echo '<br>';	}

This does print out all entries.But I need each one set to a variable.

Link to comment
Share on other sites

even closer

	$artist = mysql_query("SELECT `artist` FROM `tch_homepagenames` ORDER BY `pos` ASC");	while($array = mysql_fetch_array($artist))	{		for ($i = 0; $i < count($array); $i++)		{			$name[$i] = $array[$i];			echo $name[$i];			echo '<br>';		}	}

When printing the variables $name[0] through $name[7] later in my code only $name[0] prints out any result, and it's actually the last entry ($name[7])

Link to comment
Share on other sites

hi, why do you need them on separated variables anyway?with that you already have you can

$bigarray = array();while($array = mysql_fetch_array($artist))    {        array_push($bigarray, $array);    }//then for printing or whatever you need, especially useful when you are doing more than printingforeach($bigarray as $big){  echo $big['artist'] . '<br />';}

and for checking the $bigarray you can always print it with: print_r ($bigarray);or you can use a for to walk the array.

Link to comment
Share on other sites

What this is for...I have 8 images that can change when the owner sees fit.He wants the artist of each image have their name to appear on hover.So I created a table that enters the artists name for the correspondingimage placement.When the query has been run I need to print out each result in differentareas of the page.

Link to comment
Share on other sites

The code below prints out the array perfectly

	$artist = mysql_query("SELECT `artist` FROM `names` ORDER BY `pos` ASC");	while($array = mysql_fetch_array($artist))	{		for ($i = 0; $i < count($array); $i++)		{			$name[$i] = $array[$i];			echo $name[$i];			echo '<br>';		}	}

All 8 names print out 1 below the next.Now, when I go to print them throughout my page I get nothing.Should I not be able to print $name1 $name2 etc where I see fit?

Link to comment
Share on other sites

so i'm guessing you have before your query something like$name1 = '';...$name8 = '';i fear you might be writing over and over on a single variable, it's a good idea if could print all your $name# inside the while, this way you know if your doing right.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...