Domanda

The following code snippet checks whether the user is using a mobile browser or not.If so,the icons are shown in new lines using a tr tag.

function isMobile() {
return preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i", $_SERVER["HTTP_USER_AGENT"]);
}

Problem:

It only works for the first two times. In fact, as you can see, the function is commented out in the last 2 times it was being used as to not get a server error (500).

I also tried putting the echo part in curly brackets but this still returned a server error when all the 4 php functions below were uncommented.

<table align="center" frame='box'>
<tr>
<td align="center" colspan="20">
<a href="assignlocktouser.php"><figure><img src="Images/basicunlock.png" alt="Assign Lock to Basic User" height="64" width="64"/>
<imgcaption></p>Assign Lock to Basic User</imgcaption><figure></a>
<?php if (isMobile()) echo "</tr><tr>"; ?>
<td align="center" colspan="20">
<a href="addDevice.php"><figure><img src="Images/addDevice.png" alt="Add Device" height="64" width="64"/>
<imgcaption></p>Add Device</imgcaption><figure></a>
<?php if (isMobile()) echo "</tr><tr>"; ?>
<td align="center" colspan="20">
<a href="editSequence.php"><figure><img src="Images/editsequence.png" alt="Edit Sequence" height="64" width="64"/>
<imgcaption></p>Edit Sequence</imgcaption><figure></a>
<?php //(if isMobile()) echo "</tr><tr>"; ?>
<td align="center" colspan="20">
<a href="addLock.php"><figure><img src="Images/lock.png" alt="Add New Lock" height="64" width="64"/>
<imgcaption></p>Add New Lock</imgcaption><figure></a>
<?php //(if isMobile()) echo "</tr><tr>"; ?>
<td align="center" colspan="20">
<a href="switchLockOwner.php"><figure><img src="Images/ownerunlock.png" alt="Switch Lock Owner" height="64" width="64"/>
<imgcaption></p>Switch Lock Owner</imgcaption><figure></a>      
</tr>
</table>

Any clues as to why this is happening?

È stato utile?

Soluzione

you have your if statement the wrong way round

<?php (if isMobile()) echo "</tr><tr>"; ?>

should be

<?php if (isMobile()) echo "</tr><tr>"; ?>

I always find providing brackets best for reading if statements

so, you could do it like this

<?php if (isMobile()) {echo "</tr><tr>";} ?>

Or, if you wanted to short code it, with a real one liner if statement

<?=((isMobile()) ? "</tr><tr>" : NULL)?>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top