Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

snytax 1

Status
Not open for further replies.

maharamDan

IS-IT--Management
Mar 22, 2004
31
US
Hello I am doing a simple array fetch:

if($rss=mysql_query($query))
{
while($rs1=mysql_fetch_array($rss))
{
echo "<img src=\"image/rs1[imagename].jpg\"> ";
}


Imagename is the name of my image in the database.

My problem is that I cant get the imagename to print out in front of jpg.

How would I properly code this. If I display imagename by itself it echo's out.

Thanks Much.
 
PHP doesn't like associative array references inside strings. Use the "." contatenation operator instead.

You can't depend on PHP's understanding what $a['foo'] and $a[foo] are the same. Use the former.

If you put HTML strings in singlequotes, you don't have to escape your internal doublequotes. This makes the whole thing more readable.

Try:

Code:
if($rss=mysql_query($query))
{
   while($rs1=mysql_fetch_array($rss)) 
   {
      echo '<img src="image/' . rs1['imagename'].jpg . '">';
   }
}



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
That looks a lot better. Thanks for your help.

Only thing I added was a $ before rs1

I'll try and use this method from now on.
 
Quick question how would I add more objects from my array using this method.

echo '<img src="image/' . rs1['imagename'].jpg . '">';

Would I do this

echo $rs1[dob] . '<img src="image/' . rs1['imagename'].jpg . '">' .$rs1[month];
 
Sure.

But again, I strongly recommend that you get into the habit of using, for example, $rs1[[red]'[/red]dob[red]'[/red]] rather than $rs1[dob]

The latter may work or not without warnings or errors depending on runtime configuration directive settings. I can state that the default configuration of PHP 5.0RC2 strongly prefers having the quotes around static-string associative-array indeces.



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top