imimin Posted July 23, 2009 Share Posted July 23, 2009 I have the following portion of code that I need to generate via a DB: fadeimages[0]=["http://www.patternsofjoy.com/images/thumbs/POJ00210.jpg", "", ""] fadeimages[1]=["http://www.patternsofjoy.com/images/thumbs/POJ00222.jpg", "", ""] fadeimages[2]=["http://www.patternsofjoy.com/images/thumbs/POJ00224.jpg", "", ""] The only portion I really need to come from my DB table is (example from above) "POJ00210.jpg", "POJ00222", etc. which come from the 'img' field of the table named 'poj_products'. However, the code I have already worked on generates the whole line. What I have so far is: <?php if(!isset($_GET['cat'])){ die("Category isn't specified"); } /* Enable displaying of errors */ error_reporting(E_ALL); ini_set('display_errors', 'On'); /*$cat = mysql_real_escape_string($_GET['cat']);*/ $cat=all $get_items = "SELECT * FROM poj_products WHERE cat='$cat'"; $get_items = mysql_query($get_items); if($get_items){ $item_row = mysql_fetch_assoc($get_items); extract($item_row); echo 'fadeimages[0]=["http://www.patternsofjoy.com/images/thumbs/'.'$item_row['img']"'.', "", ""]'; }else{ die("Unable to connect to database.".mysql_error()); } ?> I am getting an error on line 15 which says: Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /homepages/27/d120150310/htdocs/poj/slideshow.php on line 15 which I halfway expected to get because I was not sure of my syntax. How can I properly syntax the above code so my output will look like the first block of code above? Thank you! Quote Link to comment Share on other sites More sharing options...
akitchin Posted July 23, 2009 Share Posted July 23, 2009 if you're going to concatenate a variable into the string, you shouldn't be using single quotes around it. this: echo 'fadeimages[0]=["http://www.patternsofjoy.com/images/thumbs/'.'$item_row['img']"'.', "", ""]'; should be: echo 'fadeimages[0]=["http://www.patternsofjoy.com/images/thumbs/'.$item_row['img'].'", "", ""]'; notice that all it's doing is breaking out of the string, appending the variable, and returning to finish the string. PS: why are you using extract() if you're just going to use the array anyway? Quote Link to comment Share on other sites More sharing options...
imimin Posted July 23, 2009 Author Share Posted July 23, 2009 Thank you! That got rid of my error but it didn't print anything either! I now get a blank screen? PS: why are you using extract() if you're just going to use the array anyway? I am modifying some code I used for something else. How would I remove extract() and make it work? Quote Link to comment Share on other sites More sharing options...
akitchin Posted July 23, 2009 Share Posted July 23, 2009 the issue actually originally was that the line that is left uncommented is a syntax error ($cat=all) - i should have picked that out the first time. comment that line out and uncomment the line above it: <?php if(!isset($_GET['cat'])){ die("Category isn't specified"); } /* Enable displaying of errors */ error_reporting(E_ALL); ini_set('display_errors', 'On'); $cat = mysql_real_escape_string($_GET['cat']); //$cat=all <-- THIS IS A SYNTAX ERROR $get_items = "SELECT * FROM poj_products WHERE cat='$cat'"; $get_items = mysql_query($get_items); if($get_items){ $item_row = mysql_fetch_assoc($get_items); echo 'fadeimages[0]=["http://www.patternsofjoy.com/images/thumbs/'.$item_row['img'].', "", ""]'; }else{ die("Unable to connect to database.".mysql_error()); } ?> you can get rid of extract() altogether and it will work just fine (i've done that in this snippet). Quote Link to comment Share on other sites More sharing options...
imimin Posted July 23, 2009 Author Share Posted July 23, 2009 When I try this suggestion, nothing still prints, just a blank page! I rather define $cat with a static variable now since I am not passing a $cat yet. Quote Link to comment Share on other sites More sharing options...
imimin Posted July 23, 2009 Author Share Posted July 23, 2009 Interesting, if I just remove $cat='all'; I get no errors, is my error reporting set up correctly? Quote Link to comment Share on other sites More sharing options...
thebadbad Posted July 23, 2009 Share Posted July 23, 2009 Did you connect to the database? Quote Link to comment Share on other sites More sharing options...
imimin Posted July 23, 2009 Author Share Posted July 23, 2009 yes Quote Link to comment Share on other sites More sharing options...
akitchin Posted July 23, 2009 Share Posted July 23, 2009 Interesting, if I just remove $cat='all'; I get no errors, is my error reporting set up correctly? i suspect with all the different versions of this snippet that have floated around, you're still making either the $cat=all or the string concatenation error. what snippet are you currently using and getting a blank page for? Quote Link to comment Share on other sites More sharing options...
imimin Posted July 23, 2009 Author Share Posted July 23, 2009 <?php if(!isset($_GET['cat'])){ die("Category isn't specified"); } /* Enable displaying of errors*/ error_reporting(E_ALL); ini_set('display_errors', 'On'); /*$cat = mysql_real_escape_string($_GET['cat']);*/ $cat='all'; $get_items = "SELECT * FROM poj_products WHERE cat='$cat'"; $get_items = mysql_query($get_items); if($get_items){ $item_row = mysql_fetch_assoc($get_items); extract($item_row); echo 'fadeimages[0]=["http://www.patternsofjoy.com/images/thumbs/'.$item_row['img'].'", "", ""]'; }else{ die("Unable to connect to database.".mysql_error()); } ?> Quote Link to comment Share on other sites More sharing options...
akitchin Posted July 23, 2009 Share Posted July 23, 2009 first, move these lines to the top: /* Enable displaying of errors*/ error_reporting(E_ALL); ini_set('display_errors', 'On'); second, it could be that your script is terminating because of a lack of GET['cat'] and your output is being hidden somehow. Quote Link to comment Share on other sites More sharing options...
imimin Posted July 23, 2009 Author Share Posted July 23, 2009 I moved these to the top: /* Enable displaying of errors*/ error_reporting(E_ALL); ini_set('display_errors', 'On'); and still got the same thing. Should I change GET['cat'] to something else?> Quote Link to comment Share on other sites More sharing options...
akitchin Posted July 23, 2009 Share Posted July 23, 2009 I moved these to the top: /* Enable displaying of errors*/ error_reporting(E_ALL); ini_set('display_errors', 'On'); and still got the same thing. Should I change GET['cat'] to something else? no, but i'd suggest changing the die() statement to echo, so you can see if it's there that the script is halting (or just comment that section out entirely). if you're accessing it directly, $_GET['cat'] won't be defined.. Quote Link to comment Share on other sites More sharing options...
ignace Posted July 23, 2009 Share Posted July 23, 2009 Weird noone noticed: $cat = isset($_GET['cat']) ? $_GET['cat'] : 'all'; $cat = mysql_real_escape_string($cat); Quote Link to comment Share on other sites More sharing options...
akitchin Posted July 23, 2009 Share Posted July 23, 2009 Weird noone noticed: $cat = isset($_GET['cat']) ? $_GET['cat'] : 'all'; $cat = mysql_real_escape_string($cat); mysql_real_escape_string() won't work if a connection to the database hasn't yet been established. Quote Link to comment Share on other sites More sharing options...
ignace Posted July 24, 2009 Share Posted July 24, 2009 Weird noone noticed: $cat = isset($_GET['cat']) ? $_GET['cat'] : 'all'; $cat = mysql_real_escape_string($cat); mysql_real_escape_string() won't work if a connection to the database hasn't yet been established. I know that but that wasn't to what I was referring. $cat = isset($_GET['cat']) ? $_GET['cat'] : 'all'; He uses $cat='all' and nowhere in his code does he retrieve the value from _GET. akitchin uncommented the line $cat = mysql_real_escape_string($_GET['cat']) (weird to point me then with a finger ) and he commented it again and left $cat='all' uncommented. Quote Link to comment Share on other sites More sharing options...
akitchin Posted July 24, 2009 Share Posted July 24, 2009 I know that but that wasn't to what I was referring. $cat = isset($_GET['cat']) ? $_GET['cat'] : 'all'; He uses $cat='all' and nowhere in his code does he retrieve the value from _GET. akitchin uncommented the line $cat = mysql_real_escape_string($_GET['cat']) (weird to point me then with a finger ) and he commented it again and left $cat='all' uncommented. yea, but i don't think that's the root cause of his issues at the moment (although that will pre-emptively fix the issue when he does sort out the current one). good catch. Quote Link to comment Share on other sites More sharing options...
imimin Posted July 27, 2009 Author Share Posted July 27, 2009 OK, back to the primary topic(I will deal with the error thing later when I need to)... I need to get the following code to print all of the lines in my DB query in the following format. Currently it is just printing the first line: WHAT OUTPUT SHOULD LOOK LIKE: fadeimages[0]=["photo1.jpg", "", ""] fadeimages[1]=["photo2.jpg", "", ""] fadeimages[2]=["photo3.jpg", "", ""] ... CODE THAT CURRENTLY JUST PRINTS FIRST LINE FROM MY DB QUERY: <?php /*$cat = mysql_real_escape_string($_GET['cat']);*/ $cat='all'; $get_items = "SELECT * FROM poj_products WHERE cat='$cat'"; $get_items = mysql_query($get_items); $item_row = mysql_fetch_assoc($get_items); extract($item_row); echo 'fadeimages[0]=["http://www.patternsofjoy.com/images/thumbs/'.$item_row['img-no_path'].'", "", ""]'; ?> Thank You! Quote Link to comment Share on other sites More sharing options...
vineld Posted July 27, 2009 Share Posted July 27, 2009 You need to loop through the result. Now you just pick the first one. while ($item_row = mysql_fetch_assoc($get_items)) { do what you want } Quote Link to comment Share on other sites More sharing options...
imimin Posted July 27, 2009 Author Share Posted July 27, 2009 OK, I set it up like this and it does not print with no errors: <?php /*$cat = mysql_real_escape_string($_GET['cat']);*/ $cat='all'; $get_items = "SELECT * FROM poj_products WHERE cat='$cat'"; $get_items = mysql_query($get_items); while($item_row = mysql_fetch_assoc($get_items)); { extract($item_row); echo 'fadeimages[0]=["http://www.patternsofjoy.com/images/thumbs/'.$item_row['img-no_path'].'", "", ""]'; } ?> Did I loop it right? Quote Link to comment Share on other sites More sharing options...
vineld Posted July 27, 2009 Share Posted July 27, 2009 Remove the semicolon from the while row. And why do you use extract? Quote Link to comment Share on other sites More sharing options...
imimin Posted July 27, 2009 Author Share Posted July 27, 2009 What I have is: <?php /*$cat = mysql_real_escape_string($_GET['cat']);*/ $cat='all'; $get_items = "SELECT * FROM poj_products WHERE cat='$cat'"; $get_items = mysql_query($get_items); while($item_row = mysql_fetch_assoc($get_items)) { extract($item_row); echo 'fadeimages[0]=["http://www.patternsofjoy.com/images/thumbs/'.$item_row['img-no_path'].'", "", ""]'; } ?> But it is not putting a <BR> between each line of generated code. It is looking like this: fadeimages[0]=["http://www.patternsofjoy.com/images/thumbs/POJ00241.jpg", "", ""]fadeimages[0]=["http://www.patternsofjoy.com/images/thumbs/POJ00256.png", "", ""]fadeimages[0]=["http://www.patternsofjoy.com/images/thumbs/POJ00257.png", "", ""]fadeimages[0]=["http://www.patternsofjoy.com/images/thumbs/POJ00264.png", "", ""] instead of: fadeimages[0]=["http://www.patternsofjoy.com/images/thumbs/POJ00241.jpg", "", ""] fadeimages[0]=["http://www.patternsofjoy.com/images/thumbs/POJ00256.png", "", ""] fadeimages[0]=["http://www.patternsofjoy.com/images/thumbs/POJ00257.png", "", ""] fadeimages[0]=["http://www.patternsofjoy.com/images/thumbs/POJ00264.png", "", ""] Quote Link to comment Share on other sites More sharing options...
vineld Posted July 27, 2009 Share Posted July 27, 2009 Then simply print a <br />? Quote Link to comment Share on other sites More sharing options...
imimin Posted July 27, 2009 Author Share Posted July 27, 2009 Thank you! When I tried that: <?php /*$cat = mysql_real_escape_string($_GET['cat']);*/ $cat='all'; $get_items = "SELECT * FROM poj_products WHERE cat='$cat'"; $get_items = mysql_query($get_items); while($item_row = mysql_fetch_assoc($get_items)) { extract($item_row); echo 'fadeimages[0]=["http://www.patternsofjoy.com/images/thumbs/'.$item_row['img-no_path'].'", "", ""]'; echo <br />; } ?> I get the following error: Parse error: syntax error, unexpected '<', expecting ',' or ';' in /homepages/27/d120150310/htdocs/poj/slideshow.php on line 33 Which is the 'echo <br />;' line. Quote Link to comment Share on other sites More sharing options...
vineld Posted July 27, 2009 Share Posted July 27, 2009 When you print text (such as html) you need to enclose it in quotes. 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.