Jump to content

echo-ing variables not working


Pavlos1316

Recommended Posts

Hello, Below is a part of my dynamicList which I use to list some items:

<?php
// I connect to my db here
$sql = mysql_query("SELECT * FROM items WHERE category='a' ORDER BY id ASC");
$productCount = mysql_num_rows($sql);
if ($productCount > 0) {
	$i=0;
    		$dynamicListBody = '<?php session_start(); ?><table width: 90%; margin-right: auto; margin-left: auto; color: #00E6AA;>';
	    while($row = mysql_fetch_array($sql)){
		    $id = $row["id"];
		    $product_name = $row["product_name"];
	$dynamicListBody .= ($i==0) ? '<tr>':'';
	$dynamicListBody .= '<td width="10%">
			      <div align="center">
				<img style="border:#666 1px solid;" src="../stock_photos/' . $id . '.png" height="80px" width="40px" alt="' . $product_name . '" />
			      </div>
			     </td>
			     <td width="35%">
			      <div align="center">
				<span class=itmttl>' . $product_name . '</span>
			       </div>
			     </td>';
	$dynamicListBody .= ($i==1) ? '</tr>':'';
	$i++;
	($i==2) ? $i=0:'';
	}
	$dynamicListBody .='</table>';
	} else {
		$dynamicListBody = "We have no items";
		}
?>

This is working perfect, but if I want to replace this:

' . $variable . '

with this:

<?php echo $variable; ?>

is not working. What is wrong here :confused:

 

Thank you

Link to comment
https://forums.phpfreaks.com/topic/239431-echo-ing-variables-not-working/
Share on other sites

Why do you have the '<?php start_session(); ?>' in the middle of your variable string? That won't do anything and will probably display on the screen.

 

Please show how you're trying to display the variable -- full context, not just a snippet.

 

Ken

Why do you have the '<?php start_session(); ?>' in the middle of your variable string? That won't do anything and will probably display on the screen.

This I didn't pay much attention because it was doing nothing and it didn't displayed in my page. I was just trying some things in order to get something else to work.

Please show how you're trying to display the variable -- full context, not just a snippet.

I've shown all I have.

 

As it is my code e.g. with:

<span class=itmttl>' . $product_name . '</span>

I get the product name displayed, but if I replace it with:

<?php echo $product_name; ?>

I get <?php echo $product_name; ?> displayed.

 

Now if you are asking how am I calling my dynamicList, I do it in another page using:

<?php echo "$dynamicListBody"; ?>

inside my <body> tags.

Why do you have the '<?php start_session(); ?>' in the middle of your variable string? That won't do anything and will probably display on the screen.

This I didn't pay much attention because it was doing nothing and it didn't displayed in my page. I was just trying some things in order to get something else to work.

Please show how you're trying to display the variable -- full context, not just a snippet.

I've shown all I have.

 

As it is my code e.g. with:

<span class=itmttl>' . $product_name . '</span>

I get the product name displayed, but if I replace it with:

<?php echo $product_name; ?>

I get <?php echo $product_name; ?> displayed.

 

Now if you are asking how am I calling my dynamicList, I do it in another page using:

<?php echo "$dynamicListBody"; ?>

inside my <body> tags.

 

Hmm, I'm not seeing the problem. It seems you are already doing it the right way. To echo a variable in the middle of a string you just concatenate it like you already have.

 

echo 'stringy string ' . $variable . ' more stringy string';

 

The only time you would do

<?php echo $variable; ?>

is if:

- You are not already working inside <?php ?> tags

- You are not already echoing/printing a string

 

Once you echo or print a string, you don't need the <?php ?> tags, just ' . . ' to concatenate.

is if:

- You are not already working inside <?php ?> tags

- You are not already echoing/printing a string

And this is why I cannot use <?php echo ?> again, because I am already using both of them.

Correct?

 

You can use echo as many times as you want, but you have to close their statements.

 

echo 'blah blah';
echo 'blah blah blah';
echo 'more blah blah blah';

Archived

This topic is now archived and is closed to further replies.

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