Jump to content

[SOLVED] Proper syntax to dynamically generate lines of code


imimin

Recommended Posts

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!

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

<?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());
}

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?>

 

Link to comment
Share on other sites

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..

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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", "", ""]

Link to comment
Share on other sites

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.

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.