Pavlos1316 Posted June 15, 2011 Share Posted June 15, 2011 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 Thank you Quote Link to comment https://forums.phpfreaks.com/topic/239431-echo-ing-variables-not-working/ Share on other sites More sharing options...
jnvnsn Posted June 15, 2011 Share Posted June 15, 2011 <?php echo $variable ?> I don't know if it's typo or you did not end the variable with a ;. Try this: <?php echo $variable; ?> Quote Link to comment https://forums.phpfreaks.com/topic/239431-echo-ing-variables-not-working/#findComment-1229988 Share on other sites More sharing options...
Pavlos1316 Posted June 15, 2011 Author Share Posted June 15, 2011 Sorry, it is a typo... I have the ";" ending Quote Link to comment https://forums.phpfreaks.com/topic/239431-echo-ing-variables-not-working/#findComment-1229989 Share on other sites More sharing options...
kenrbnsn Posted June 15, 2011 Share Posted June 15, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/239431-echo-ing-variables-not-working/#findComment-1230012 Share on other sites More sharing options...
Pavlos1316 Posted June 15, 2011 Author Share Posted June 15, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/239431-echo-ing-variables-not-working/#findComment-1230015 Share on other sites More sharing options...
redixx Posted June 15, 2011 Share Posted June 15, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/239431-echo-ing-variables-not-working/#findComment-1230047 Share on other sites More sharing options...
Pavlos1316 Posted June 15, 2011 Author Share Posted June 15, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/239431-echo-ing-variables-not-working/#findComment-1230053 Share on other sites More sharing options...
redixx Posted June 15, 2011 Share Posted June 15, 2011 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'; Quote Link to comment https://forums.phpfreaks.com/topic/239431-echo-ing-variables-not-working/#findComment-1230055 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.