Fallen_angel Posted January 30, 2007 Share Posted January 30, 2007 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 databaseinclude "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 Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted January 30, 2007 Share Posted January 30, 2007 whats the error?Ted Quote Link to comment Share on other sites More sharing options...
Fallen_angel Posted January 30, 2007 Author Share Posted January 30, 2007 sorry i must have been editing as you posted the error I am getting is unexpected T_VARIABLE on line 14 Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted January 30, 2007 Share Posted January 30, 2007 [code]<?phpforeach ($row{$i++; $image[$i]=$row[$i]['imagefile'];})//I find this a very weird expression.?>[/code]What are you trying to achieve here? and btw, i think this is what is causing you the trouble.Ted Quote Link to comment Share on other sites More sharing options...
Fallen_angel Posted January 30, 2007 Author Share Posted January 30, 2007 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 Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted January 30, 2007 Share Posted January 30, 2007 my instinct tells me to use while here:[code]<?php//Connect To databaseinclude "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 Quote Link to comment Share on other sites More sharing options...
Nameless12 Posted January 30, 2007 Share Posted January 30, 2007 You are doing it wrongforeach ($array_or_object as $value){ echo $value;}or alternatively foreach ($array_or_object as $key => $value){ echo $key . ': ' . $value;}you were forgetting the AS portion Quote Link to comment Share on other sites More sharing options...
Jenk Posted January 30, 2007 Share Posted January 30, 2007 the 'as' statement is not necessary in a foreach loop. Quote Link to comment Share on other sites More sharing options...
Nameless12 Posted January 30, 2007 Share Posted January 30, 2007 Yes it is.<?phpforeach (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 Quote Link to comment Share on other sites More sharing options...
Jenk Posted January 30, 2007 Share Posted January 30, 2007 Sorry, you are correct. For some reason I thought we were discussing the key => variable assignment. Quote Link to comment Share on other sites More sharing options...
Nameless12 Posted January 30, 2007 Share Posted January 30, 2007 np Quote Link to comment Share on other sites More sharing options...
Fallen_angel Posted January 31, 2007 Author Share Posted January 31, 2007 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 databaseinclude "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 Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 31, 2007 Share Posted January 31, 2007 $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] Quote Link to comment Share on other sites More sharing options...
Fallen_angel Posted January 31, 2007 Author Share Posted January 31, 2007 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 onethanx again for the help Quote Link to comment Share on other sites More sharing options...
redarrow Posted January 31, 2007 Share Posted January 31, 2007 for a real slide show use javascript/ajax or flash but for a random image use php.good luck. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.