Reputation: 61
I have this PHP snippet that makes an <li>
with some id which is chosen by mysqli_query
fetched row. It looks like this:
while($row=mysqli_fetch_array($run_query)){
$name=$row['company_name'];
echo "<li><a href='#' id=".$name." class='category_sidebar2'>$name</a></li>";
}
When the $name
is a single entity like Dell or Facebook, it creates the <li>
just fine, but when the $name
is a space-separated value like "Tata Steel" or something, it creates the <li>
with the id
just the first value before the space.
For example, if $name
is "Tata Steel", it echoes
<li><a href='#' id='Tata'></li>
and not
<li><a href='#' id='Tata Steel'></li>
How do I get around this?
Upvotes: 0
Views: 334
Reputation: 780673
You need to put quotes around an attribute if it contains spaces. Otherwise, the space will end the attribute. You should just get in the habit of always putting quotes around attribute (like you did for href
and class
).
while($row=mysqli_fetch_array($run_query)){
$name=$row['company_name'];
echo "<li><a href='#' id='".$name."' class='category_sidebar2'>$name</a></li>";
}
However, as mentioned in the comments, spaces around allowed in the id
attribute. You could replace the space with some other character, e.g.
$name = str_replace(' ', '_', $row['company_name']);
Upvotes: 1