John Doe
John Doe

Reputation: 2060

String interpolation with an array

I am trying to create a link with php, but am having some difficulty. Can someone help me with this. I want the link to go to yourteam.php with the title being whatever the variable $row['User_ID'] is....

echo "<tr bgcolor=\"#D1D1D1\"><td>" . "<a href=\"yourteam.php\">$row['User_ID']</a>" . "</td><td><b>" . $row['Correct_Picks'] . " </b> /" . $maxcorrectpicks . "</td><td>" . $row['Points'] . "</td></tr>";

Upvotes: 1

Views: 771

Answers (3)

Saad Imran.
Saad Imran.

Reputation: 4530

I know people suggested using single quotes, but I like to use double quotes on my HTML attributes and use indentation within my echo statements for readibility. Here's how I would do it...

echo "<tr bgcolor=\"#D1D1D1\">";
echo "    <td><a href=\"yourteam.php\">{$row['User_ID']}</a></td>";
echo "    <td><b>{$row['Correct_Picks']}</b>{$maxcorrectpicks}</td>";
echo "    <td>{$row['Points']}</td>";
echo "</tr>";

Upvotes: 0

The Alpha
The Alpha

Reputation: 146219

Try this

echo "<tr bgcolor='#D1D1D1'><td><a href='yourteam.php'>".$row['User_ID']."</a></td><td><b>" . $row['Correct_Picks'] . "</b>" . $maxcorrectpicks . "</td><td>" . $row['Points'] . "</td></tr>";

I suggest you to use <strong></strong> instead of <b></b> and also you can use inline style as style='background-color:#D1D1D1;' instead of bgcolor.

Upvotes: 0

Musa
Musa

Reputation: 97707

When using a array item in a double quoted string you leave out any quotes

echo "<tr bgcolor=\"#D1D1D1\"><td>" . "<a href=\"yourteam.php\">$row[User_ID]</a>" .

you can also wrap the variable in {}

echo "<tr bgcolor=\"#D1D1D1\"><td>" . "<a href=\"yourteam.php\">{$row['User_ID']}</a>" .

Upvotes: 1

Related Questions