0

Total noob, having trouble using a variable to loop through row. I'd like to print the contents of the column header_en. So I:

Set $lang to en, connect to db and SELECT *

Then:

while($row = mysql_fetch_array( $result )) {
    echo "<h3>Headline is: $row['header_$lang]</h3>";
    echo "<p>Author: $row[author]</p>";
} 

I've been searching for hours, tried concatenating around the $lang variable, all sorts of stuff... no luck. My syntax-fu isn't all that strong yet.

Any pointers?

1
  • i believe answers below have solved your problem , if yes than mark one of them as answered Commented Feb 26, 2012 at 18:29

3 Answers 3

2

Try

echo "<h3>Headline is: " . $row['header_'.$lang] . "</h3>";

or even

$headerLanguage = 'header_'.$lang;
while($row = mysql_fetch_array( $result )) { 
    echo "<h3>Headline is: " . $row[$headerLanguage] . "</h3>"; 
    echo "<p>Author: $row[author]</p>"; 
} 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot! Ed's solution below works too, but Mark was faster on the draw. For anybody struggling: my error was missing the final .
1

Perhaps you need

echo "<h3>Headline is: ".$row['header_'.$lang]."</h3>"; 
echo "<p>Author: ".$row['author']."</p>"; 

1 Comment

Fix your first variable name ;) You missed a "_" sign.
0

If you use quotes in php you need to know this:

echo 'single quotes are faster, but dont see variables ' . $variable;
echo "double quotes can see them like so: $variable";
echo "to show array in doble quotes use brackets {$array['key']}";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.