Pages: [1]   Go Down
Send this topic | Print
Author Topic: Syntax Help for Echoing Variables in PHP  (Read 573 times)
MTGap!
Hyper-Active Member
***
Offline Offline

Posts: 352



WWW
« on: November 26, 2008, 06:27:54 AM »

Well I always have syntax problems and right now I'm getting an unexpected T_VARIABLE in my file.

Code:
foreach ($images_explode as $image) {

echo "<img src='.$image' alt='.$data['product_name']'>";

}

How exactly do I write those variables in there?

Logged
Confuser
Creator of eoCMS
Loyal 110MB Member
*******
Offline Offline

Posts: 3128


The Forum's Angel


WWW
« Reply #1 on: November 26, 2008, 06:31:51 AM »

You have used " so u must use " to escape
Code:
foreach ($images_explode as $image) {

echo "<img src='".$image."' alt='".$data['product_name']."'>";

}

EDIT: When putting variables like $image inside an echo, you can either use " or '. If you use " at the begining then there is no need to escape. If you use ' then you need to escape by doing ' . $image . '.
However that does not apply to something like $data['product_name'] as the ' messes it up so you must always escape those either ' . $data['product_name'] . ' or " . $data['product_name'] . " You can wrap it around { } when using " but i think that is being depreciated in PHP 6 (not 100% sure), if it is then it would be a bad habit Duh

" parses PHP variables where as ' does not

Also when using " all HTML attributes must have ' like src='lol.jpg' instead of src="lol.jpg". Or you could just put a \ before the ' so src=\'lol.jpg\' for " you can use src=\"lol.jpg\"
« Last Edit: November 26, 2008, 06:39:41 AM by confuser » Logged

tpog
Super Authority member
******
Online Online

Posts: 1540


WWW
« Reply #2 on: November 26, 2008, 06:39:36 AM »

If eventually, you want to output this as html, you may need quotes in the html syntax. Depending on whether or not these are already included in variables, might be easier to use single quotes to delimit the php string allowing you to use double quotes in the html string.

Code:
foreach ($images_explode as $image) {

echo '<img src="'.$image.'" alt="'.$data['product_name'].'">';

}

Overlapped with confuser, apologies
« Last Edit: November 26, 2008, 06:43:09 AM by tpog » Logged
MTGap!
Hyper-Active Member
***
Offline Offline

Posts: 352



WWW
« Reply #3 on: November 26, 2008, 06:53:20 AM »

Well thanks as far as I know it works now, but different topic: Why am I getting these:

Notice: Undefined index: product_images in /www/110mb.com/b/r/i/c/k/c/l/u/brickclub/htdocs/lego_product_database/product.php on line 58

Notice: Undefined index: product_name in /www/110mb.com/b/r/i/c/k/c/l/u/brickclub/htdocs/lego_product_database/product.php on line 63

Notice: Undefined index: product_name in /www/110mb.com/b/r/i/c/k/c/l/u/brickclub/htdocs/lego_product_database/product.php on line 64

Code:
// Retrieves all MySQL data.

if (isset($_GET['product_id']) && isnum($_GET['product_id'])) {

$result = dbquery("select * from ".$db_prefix."product_database WHERE product_id='".$_GET['product_id']."'");

if (dbrows($result)) {

$data = dbarray($result);

$product_info = array(

"product_id" => $data['product_id'],

"product_name" => $data['product_name'],

"product_description" => $data['product_description'],

"product_year" => $data['product_year'],
"product_main_image" => $data['product_main_image'],
"product_images" => $data['product_images'],
"product_shop" => $data['product_shop'],

);

} else {

redirect(FUSION_SELF);

}

}

// Explodes '@' from product images.
$images_explode = explode("@",$data['product_images']);


// Data is echoed
$text = "Statistics";

opentablelpd1($data['product_name']);

foreach ($images_explode as $image) {

echo "<img src=".$image." alt=".$data['product_name'].">";
}

tablelpd($text);
echo "Test";
closetablelpd2();


I've had it before with those variables and it worked, did I mess something up somewhere?

Just a note: If it looks like PHP-Fusion, that's because it is part of it...
Logged
Confuser
Creator of eoCMS
Loyal 110MB Member
*******
Offline Offline

Posts: 3128


The Forum's Angel


WWW
« Reply #4 on: November 26, 2008, 03:47:22 PM »

Well you are only seeing those because error_reporting is on. Those might be showing because product_images might not exist in the database?
Logged

Dark Raito
Dark Templar
Loyal 110MB Member
*******
Offline Offline

Posts: 2280


i seems to prefer to read more than to post more..


WWW
« Reply #5 on: November 26, 2008, 05:57:58 PM »

use this instead, but it won't help you if you already deleted the field namely, product_name and product_images from the table product_database.


Code:
// Retrieves all MySQL data.

if (isset($_GET['product_id']) && isnum($_GET['product_id'])) {

// just a simple method to escape errors.
$product_info['product_name'] = "";
$product_info['product_images'] = "";

$result = dbquery("select * from ".$db_prefix."product_database WHERE product_id='".$_GET['product_id']."'");

if (dbrows($result)) {

$data = dbarray($result);

$product_info = array(

"product_id" => $data['product_id'],
"product_name" => $data['product_name'],
"product_description" => $data['product_description'],
"product_year" => $data['product_year'],
"product_main_image" => $data['product_main_image'],
"product_images" => $data['product_images'],
"product_shop" => $data['product_shop'],

);

} else {

redirect(FUSION_SELF);

}

}

// Explodes '@' from product images.
$images_explode = explode("@",$product_info['product_images']);


// Data is echoed
$text = "Statistics";

opentablelpd1($product_info['product_name']);

foreach ($images_explode as $image) {

echo "<img src=".$image." alt=".$product_info['product_name'].">";
}

tablelpd($text);
echo "Test";
closetablelpd2();

specially this part
Code:
echo "<img src=".$image." alt=".$data['product_name'].">";
Logged


110MB.com Help!
NubsPixel.com
Quote from: 'A Wise person'
have you ever faced failures?

never. because there is no such thing as failure.
Only lessons that make you a more better person.
Primefalcon
Linux Acolyte
Loyal 110MB Member
*******
Online Online

Posts: 4645


Follow the path of Linux, it will lead you....


« Reply #6 on: November 26, 2008, 06:03:55 PM »

as an addition to what's been said above... you could use a herdoc syntax too if your doing a lot of html with "'s for example


Code:
<?php


echo <<<HEYTHISISADOCUMENT

now simply echo out any content you want here using " ' and you can even use variables :-)

HEYTHISISADOCUMENT;

?>


instead of hetthisisadocument you can change that to whatever you want however... you must have the <<< before it and the closing one must be on a line by itself with nothing else, not even whitespace before it
Logged

Dropbox is an amazing cloud storage backup solution, get a free 2.25 gigabytes of storage by using THIS LINK

For Tips on Runescape, Visit Marlaine's Musings For Tips on Just about anything Visit Marlainemarie at eHow
fiate2000
Authority Member
****
Offline Offline

Posts: 682


WWW
« Reply #7 on: November 26, 2008, 10:08:55 PM »

as an addition to what's been said above... you could use a herdoc syntax too if your doing a lot of html with "'s for example


Code:
<?php


echo <<<HEYTHISISADOCUMENT

now simply echo out any content you want here using " ' and you can even use variables :-)

HEYTHISISADOCUMENT;

?>


instead of hetthisisadocument you can change that to whatever you want however... you must have the <<< before it and the closing one must be on a line by itself with nothing else, not even whitespace before it

Cool, i did not know that! It will make some of my codes really easy and less messy. Thanks a lot  cheesy
Logged

MTGap!
Hyper-Active Member
***
Offline Offline

Posts: 352



WWW
« Reply #8 on: November 27, 2008, 12:59:34 AM »

Well I copied and pasted the exact code from your post Dark Raito and tested it out:



You can see in this screenshot that those fields are there:



There has to be a dumb mistake I made somewhere... I'm confused why the product_name doesn't work anymore especially since it was working correctly recently.

Just a note: http://www.brickclub.co.cc/lego_product_database/product2.php?product=1000 That is the product_id I'm looking at, 1000

Logged
fiate2000
Authority Member
****
Offline Offline

Posts: 682


WWW
« Reply #9 on: November 27, 2008, 02:09:36 AM »

The error messages relate to product_info, not  product_name

and the error is generated because you do not have a field named product_info (did you mean product_description?)
Logged

MTGap!
Hyper-Active Member
***
Offline Offline

Posts: 352



WWW
« Reply #10 on: November 27, 2008, 02:26:41 AM »

use this instead, but it won't help you if you already deleted the field namely, product_name and product_images from the table product_database.


Code:
// Retrieves all MySQL data.

if (isset($_GET['product_id']) && isnum($_GET['product_id'])) {

// just a simple method to escape errors.
$product_info['product_name'] = "";
$product_info['product_images'] = "";

$result = dbquery("select * from ".$db_prefix."product_database WHERE product_id='".$_GET['product_id']."'");

if (dbrows($result)) {

$data = dbarray($result);

$product_info = array(

"product_id" => $data['product_id'],
"product_name" => $data['product_name'],
"product_description" => $data['product_description'],
"product_year" => $data['product_year'],
"product_main_image" => $data['product_main_image'],
"product_images" => $data['product_images'],
"product_shop" => $data['product_shop'],

);

} else {

redirect(FUSION_SELF);

}

}

// Explodes '@' from product images.
$images_explode = explode("@",$product_info['product_images']);


// Data is echoed
$text = "Statistics";

opentablelpd1($product_info['product_name']);

foreach ($images_explode as $image) {

echo "<img src=".$image." alt=".$product_info['product_name'].">";
}

tablelpd($text);
echo "Test";
closetablelpd2();

specially this part
Code:
echo "<img src=".$image." alt=".$data['product_name'].">";

Well I did what Dark Ratio had suggested, that is where product_info came from...

Unless am I supposed to fill in those variables:

Code:
// just a simple method to escape errors.
$product_info['product_name'] = "";
$product_info['product_images'] = "";
?? It didn't work the last time with my original code, but I see those fields right there in the 110dbmanager... ??
Logged
fiate2000
Authority Member
****
Offline Offline

Posts: 682


WWW
« Reply #11 on: November 27, 2008, 03:21:07 AM »

Ok, I understand now. Try

Code:
$product_info = array("product_id" => $data['product_id'], "product_name" => $data['product_name']);

in place of

Code:
$product_info['product_name'] = "";
$product_info['product_images'] = "";

Logged

MTGap!
Hyper-Active Member
***
Offline Offline

Posts: 352



WWW
« Reply #12 on: November 27, 2008, 03:37:00 AM »

I'm confused with that, that just makes another array from an array. Why won't it work with the $data array, it has in the past. I had to of made a mistake somewhere...
Logged
Pages: [1]   Go Up
Send this topic | Print
Jump to: