Wrong php code in this line

The first line of the table is throwing up an error in the first part (the link)

Could you tell me what I am doing wrong, please?

  <tr>
    <td><?php echo($chain == 'Accor') ? "<a href="/request/index.php?id=Accor"> <img src="<?php echo $image; ?>" alt="" /></a></td>
    <td><?php echo $chain; ?></td>
    <td><?php echo $country; ?></td>
    <td><?php echo $city; ?></td>
    <td><img src="<?php echo($top == 'y') ? "Gre.png" : "Red.png"; ?>" alt=""></td>
" alt=""> " alt="">

There is no else condition on the code in the first td.

Hi felgall

I am battling with this one since a couple of days ago.

I thought (think) that I might solve my problem by extending this line with 20 else conditions

do you think that is viable?

} else { "<a href="/request/index.php?id=Sheraton"

} else { "<a href="/request/index.php?id=Hilton"

} else { "<a href="/request/index.php?id=IHG"

Etc, etc...

But the code above is otherwise correct?

Thank you

there are lots of string errors (T_ENCAPSED_AND_WHITESPACE) as can be seen by the code highlighting.

Hi Dormilich

Iā€™m learning something new! You mean the red lettering in the posts above mean wrong code?

This is what I have so far and does not work (surprise, surprise!)

     <td><?php echo($chain == 'Accor') ? "<a href="/request/index.php?id=Accor"></a> } else { "<a href="/"></a>;?><img src="<?php echo $image;?>" alt="" /></a></td>

I donā€™t understand php and canā€™t see what is wrong (possibly everythingā€¦)

Could you point me in the right direction so that I can decide if this sort code is of any use?

Thank you

If you have a look at the quotes, you can see theyā€™re not balancing correctly:

<td><?php echo($chain == 'Accor') ? "<a href="/request/index.php?id=Accor"></a> : "<a href="/"></a>;?><img src="<?php echo $image;?>" alt="" /></a></td>

You open and close single-quotes around your test condition, thatā€™s correct. But then the true condition return isnā€™t quoted correctly - thereā€™s a double-quote which will start the return string, then another that will end it where you probably want it to be the start of the URL. I think it should be nearer this:

<td><?php echo($chain == 'Accor') ? '<a href="/request/index.php?id=Accor"></a>' : '<a href="/"></a>';?><img src="<?php echo $image;?>" alt="" /></a></td>

For consistency there, Iā€™ve used single quotes to surround strings so that I can include double-quotes within those strings. As Iā€™m not expecting PHP to substitute variable values inside any of those strings, it will work.

Iā€™m not sure your HTML is correct, though - you seem to open and close a hyperlink with no content, then close it again after youā€™ve output the image. If your intention was to use the image as the link, then this might be better, by dropping the closing tag from within your conditional string.

<td><?php echo($chain == 'Accor') ? '<a href="/request/index.php?id=Accor">' :  '<a href="/">';?><img src="<?php echo $image;?>" alt="" /></a></td>

You canā€™t mix the ternary operator (the [ condition ? true-operation : false operation ] construction) with if-then-else in the way you are talking about, as far as I know.

1 Like

Hi droopsnoot

You saved my life! It works now, but the idea is that I now need to continue with something like 20 }else{

Do you think that will work?

Well, there is nothing like trying, and Iā€™m afraid I did something wrong again!

<td><?php echo($chain == 'Accor') ? '<a href="/request/index.php?id=Accor">' : '<a href="/request/index.php?id=IHG">' : '<a href="/">';?><img src="<?php echo $image;?>" alt="" /></a></td>

It seems to work with 2 redirections only. What do I need to do to continue.? I though you only need to repeat else, but maybe I need some other code ā€¦ I know nothing of php

You canā€™t do that in that way with the ternary operator. In fact how could you? You provide a condition which is tested, the first operation is done if the test returns true, otherwise the other operation is done. So under what circumstances would the third operation be done, or the fourth or any other? The test is either true or not, so it only has two outcomes.

If you want to check for multiple different conditions, then youā€™d need to use a series of if-then-else or, better in my mind, a switch() construction.

But, if your links are just using the value of $chain as the id parameter, why not do something like this?

<td><?php echo ($chain != '') ? '<a href="/request/index.php?id=' . $chain . '">' : '<a href="/">';?><img src="<?php echo $image;?>" alt="" /></a></td>

If thereā€™s anything at all in the $chain variable, it will be used as the id. If there is not, it will link to the default index.

Note that Iā€™ve assumed here that $chain will exist. If there is a chance it does not, you must check for that so as to not get errors for a non-existent variable.

Hi droopsnoot

That was th way I coded it first, but unfortunately the headers sent are wrong

Looking at FF Browser Console, when you press the Accor link instead of getting a clean

GET http://pintotours.net/request/index.php?id=Accor

you get

GET http://pintotours.net/request/index.php?id='.%20$chain%20.'

and the redirect fails diverting to the default page which is one of my pages

Could you help me with the if-then-else or, better in my mind, a switch() construction. , please

Once you get it up to 3 I will do the rest

Thatā€™ll be quotes again - if you look at the link you can see that itā€™s actually putting the string ā€œ$chainā€ in there, rather than substituting the value. If it really is based on that value, any other way would be excess.

Remember that if you surround the entire string in single-quotes, as I mentioned above, php doesnā€™t substitute variable values, it only does this when you surround with double quotes. So you either have to concatenate the variable, or surround the whole thing with double quotes - which you canā€™t do here because you use double-quotes inside the string as well. Well, you can, but you have to escape the quotes inside the string, which adds some confusion in my mind, I prefer to keep it neat.

1 Like

Hi I need to go out for 1/2 an hour. Iā€™ll try the quotes, then.

Thank you very much. I spent the night thinking about this little ā€œproblemā€ā€¦

I think this was the code

<tr>
<td><a href="/request/index.php?id='. $chain .'"><img src="<?php echo $image; ?>" alt="" /></a></td> 
<td><?php echo $chain; ?></td>
<td><?php echo $country; ?></td>
<td><?php echo $city; ?></td>
<td><img src="<?php echo($top == 'y') ? "Gre.png" : "Red.png"; ?>" alt=""></td>
<td><img src="<?php echo($medium == 'y') ? "Gre.png" : "Red.png"; ?>" alt=""></td>
<td><img src="<?php echo($low == 'y') ? "Gre.png": "Red.png";  ?> " alt=""></td>

Iā€™ve just tried the code you gave above and it works! on the first one. Iā€™llhave to test further when I get back.

My daughter has an exam and she forgot half her work at homeā€¦ I have to drive to the school.

<td><a href="/request/index.php?id='. $chain .'"><img src="<?php echo $image; ?>" alt="" /></a></td> 

Well, thereā€™s no php echo in there, so itā€™s no wonder it produced the wrong link.

<td><a href="/request/index.php?id=<?php echo $chain; ?>"><img src="<?php echo $image; ?>" alt="" /></a></td> 
1 Like

Hi droopsnoot

Magic!

Look at http://pintotours.net/TEMP/FINAL/input.html Enter ā€œBrusselsā€ (the only way of getting several cahins at the moment) and then click on the imagesā€¦

Pure magic!

Many thanks. I will sleep well tonight!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.