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

Link to comment
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.

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.

Link to comment
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.

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.

Link to comment
Share on other sites

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';

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.