Jump to content

Single and double quotes for echo lists of a tags retrieving data from dbase


Go to solution Solved by Barand,

Recommended Posts

I have a navigation list displaying which is a mix of html and php, everything is working fine however now I want to convert this block of code into a function but am having major problems with quotes.

The line of code I currently have is 

$data = $db->query("SELECT * FROM menu")->fetchAll(PDO::FETCH_ASSOC);

foreach ($data as $row) { ?>
     <li><a href="<?php echo $row['url']; ?>" title="<?php echo $row['title']; ?>"><?php echo $row['icon'] . ' ' . $row['header']; ?></a></li>

<?php
	}
?>

As I say everything works using the above but now I am trying to echo the full li out and am having major issues with single and double quotes.

I currently have 

echo "<li><a href='#' title='the title'><i class='fas fa-user site-nav--icon'></i> Help</a></li>";

Now I am trying to use the $row['url'], $row['title'], $row['icon'] & $row['header'] as per the top example but I cannot get the combination of quote marks correct, whether to use double, single or a combination.

I would be grateful if someone could suggest the correct syntax for the a tag then I can work through the rest.

Thanks

Link to post
Share on other sites
  • Solution

Use {..} around the complex variables, e.g.

echo "<li><a href='{$row['url']}' title='{$row['title']}'><i class='fas fa-user site-nav--icon'></i> Help</a></li>";
                   ^___________^         ^_____________^

 

Link to post
Share on other sites

Embedding PHP like that is confusing, hard to debug and unnecessary. Use double quotes but escape those you want to keep as HTML and be consistent. Single quotes are OK for indices.

<li>
<?php
echo "<a href=\"$row['url']\" title=\"$row['title']\">$row['icon'].\" \".$row['header']";
?>
</a></li>

 

Link to post
Share on other sites
17 minutes ago, Barand said:

Use {..} around the complex variables, e.g.



echo "<li><a href='{$row['url']}' title='{$row['title']}'><i class='fas fa-user site-nav--icon'></i> Help</a></li>";
                   ^___________^         ^_____________^

 

Thanks for this. It gives me part of the solution however I get the echo and quotes printed on screen. When I look at the colour coding in brackets the closing single quote, square bracket and closing brace are highlighted in red

Edited by Skorpio
Link to post
Share on other sites
16 minutes ago, gw1500se said:

Embedding PHP like that is confusing, hard to debug and unnecessary. Use double quotes but escape those you want to keep as HTML and be consistent. Single quotes are OK for indices.


<li>
<?php
echo "<a href=\"$row['url']\" title=\"$row['title']\">$row['icon'].\" \".$row['header']";
?>
</a></li>

 

Thanks for this however as I say I want to put the whole navigation code block into a function so the closing a and li would be an issue. From trying the code I get 

Quote

Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:\xampp\htdocs\kevinpowell1\index.php on line 44

 

Link to post
Share on other sites
19 minutes ago, gw1500se said:

Embedding PHP like that is confusing, hard to debug and unnecessary. Use double quotes but escape those you want to keep as HTML and be consistent. Single quotes are OK for indices.


<li>
<?php
echo "<a href=\"$row['url']\" title=\"$row['title']\">$row['icon'].\" \".$row['header']";
?>
</a></li>

 

Simple and easy to debug it may be, but unfortunately it doesn't get past the syntax parser

Quote

Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) inC:\inetpub\wwwroot\test\noname24.php on line 2

 

Link to post
Share on other sites
47 minutes ago, Barand said:

Use {..} around the complex variables, e.g.


echo "<li><a href='{$row['url']}' title='{$row['title']}'><i class='fas fa-user site-nav--icon'></i> Help</a></li>";
                   ^___________^         ^_____________^

 

Thanks, when I put in the additional $row information I again added too many quotes. This has solved the issue which has resulted in the following code

foreach ($data as $row) { 
     echo "<li><a href='{$row['url']}' title='{$row['title']}'><i class='{$row['icon']}'></i> {$row['header']}</a></li>";
}

If I use the quotes around the {$row['header']} -> '{$row['header']}' they print out on screen. Removing the 2 single quotes has solved the issue and when I view source I get the following

<li><a href='?page=home' title='Home Page'><i class='fas fa-home site-nav--icon'></i> Home</a></li>

 

Link to post
Share on other sites
58 minutes ago, Skorpio said:

echo "<li><a href='{$row['url']}' title='{$row['title']}'><i class='{$row['icon']}'></i> {$row['header']}</a></li>";

Just a thought, but did you ever consider using sprintf() instead of echo()

sprintf( '<li><a href="%s" title="%s"><i class="%s"></i> %s</a></li>' 
   , $row['url'] 
   , $row['title'] 
   , $row['icon'] 
   , $row['header'] 
   ); 

Regards,   Phill  W. 

  • Like 1
Link to post
Share on other sites
1 minute ago, Phi11W said:

Just a thought, but did you ever consider using sprintf() instead of echo()


sprintf( '<li><a href="%s" title="%s"><i class="%s"></i> %s</a></li>' 
   , $row['url'] 
   , $row['title'] 
   , $row['icon'] 
   , $row['header'] 
   ); 

Regards,   Phill  W. 

I hadn't thought of any alternatives other than return and echo the function which I have now,

function navmenu($db){
        
   $data = $db->query("SELECT * FROM menu")->fetchAll(PDO::FETCH_ASSOC);
        
   foreach ($data as $row) { 
      return "<li><a href='{$row['url']}' title='{$row['title']}'><i class='{$row['icon']}'></i> {$row['header']}</a></li>";
   }
           
}

then

// Navigation Menu Function
echo navmenu($db);

 

Link to post
Share on other sites

Hello Skorpio,

I just want to add a method to accomplish the task of separating single and double quotes: use single quotes to echo html with double quotes then concatenate variables, arrays and constants. Using your original code and the code suggested by Barand as a basis:

echo '<li><a href="' . $row['url'] . '" title="' . $row['title'] . '"><i class="fas fa-user site-nav--icon"></i> Help</a></li>';

it is probably what you were looking to accomplish.

Barand is a superb coder! I don't know gw1500se but gw1500se is also a pro, so i am not trying to step on anyones toes here. I see what you are doing and i think i can help you see what you should've been doing instead. Some people do not like spaghetti code (mixing php with html) so use whatever method suits you best. Again, i am just trying to add a method that is successful for me whenever i use the pasta :-)

Best wishes,

John

Link to post
Share on other sites

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.