Jump to content

Asistance debuging a For Each statement


Fallen_angel

Recommended Posts

I have been fiddling with the bellow for each statement for a few days now and just can't see what i'm getting wrong

the purpose of this part of the script is to simply make the value of $i in the array go up from 0 to the max number of results

[code]<?php
//Connect To database
include "connect.php";
$id = $_GET['id'];

//collects Data
$data=mysql_query(" SELECT imagefile FROM database where id='$id' ")
or die(mysql_error());
$row=mysql_fetch_array($data) ;
$i=0 ;
foreach ($row
{
$i++
  $image[$i]=$row[$i]['imagefile'] ;
  }
)
?>[/code]

when I say that i cant work it out i mean that i keep getting syntax errors for the most part they have been unexpected T_VARIABLE  errors however a few times they have been various other errors cause i have tried things out The part that is giving me trouble is the for each statement itself none of the other stuff there

thanx allot to anyone that can help in advance
Link to comment
Share on other sites

I was trying to do what was suggested in the bellow link

[url=http://www.phpfreaks.com/forums/index.php/topic,123109.msg508469.html#msg508469]http://www.phpfreaks.com/forums/index.php/topic,123109.msg508469.html#msg508469[/url]


basicly the end result is that I want to create a slide show , the array is meant to contain each imagefile name from each relevant row ,

the above code is basicly just to change $i from 0 to what ever number, incrementing one number relivant per row 

for example the first would become

image[0]  would be the first , image[1] woudl be the seccond and image[8] would be the final


hope that makes sense





Link to comment
Share on other sites

my instinct tells me to use while here:
[code]
<?php
//Connect To database
include "connect.php";
$id = $_GET['id'];

//collects Data
$data=mysql_query(" SELECT imagefile FROM database where id='$id' ")
or die(mysql_error());
$i = 0;
while ($row=mysql_fetch_array($data))
{echo $i;$i ++;}
?>[/code]
try this, and see if the outcome is what you want.
Ted
Link to comment
Share on other sites

Yes it is.


<?php
foreach (array('one', 'two'))
{
echo 'a';
}
?>

creates the following error

<b>Parse error</b>:  syntax error, unexpected ')' in <b>PHPDocument2</b> on line <b>2</b><br />

not to mention the fact it shows up as invalid code in zendStudio and I have never seen any documentation showing it with out the as statement stated before
Link to comment
Share on other sites

thanx allot guys  the as statement was where i was totally going wrong , I saw it used in the php manual and 

I now have it so that I get no errors with the bellow code

[code]<?php
//Connect To database
include "connect.php";
$id = $_GET['id'];
//collects Data
$data=mysql_query(" SELECT imagefile FROM strain_images where strain_id='$id' ")
or die(mysql_error());
$row=mysql_fetch_array($data) ;
$i=0 ;
foreach ($row as $image) {
  $i++ ;
  $image[$i]=$row[$i]['imagefile'];
  }
 
echo "<img src='./uploads/$image[0]'>"

?>

[/code]

however I think I am a little confused as to how the value gets echoed or mabey i set something wrong ( perhaps how i used as )  because when I do what i would normally do to insert an array into a database or echo it on the screen it simply doesn't work

the above code for example gives me the letter b as the value of $image[0] which is not what it's database value is ,  if I change the 0 int to 1 though then I get a instead of b

any idea's ?


thanx again in advance

Link to comment
Share on other sites

$image is a string, so doing $image[0] gets the first letter in the string.
What are you trying to do? Print out many images? If it should be just one image, there is no need for a foreach. If it is many, you'll need to move the echo into the loop, and just print $image, not $image[0]
Link to comment
Share on other sites

Hi thanx for the reply

what I am actually trying to do is que up a series of images , to be used in a slide show

the webapp I have made is a tutorial guide and I want the users to be able to click on a link in the tutorial and view a slide show for all the images relating to it ,

I want to display these one by one

thanx again for the help
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.