PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
AdrenalineSeed
Forum Newbie
Posts: 24 Joined: Sat Oct 09, 2010 5:54 pm
Post
by AdrenalineSeed » Sat Oct 09, 2010 6:02 pm
I want to make sure there is data in my array before using it. So that I do not get this error:Undefined variable: signedup in C:\wamp\www\Tournaments\tinfo.htm.php on line 33
Here is what I tried, which is odd because I am using the if statement twice.. but I couldn't see any other way to do it. Anyways I get syntax error and I just don't think I am going about this right.
Code: Select all
<?php if (isset($signedup))
{ foreach ($signedup as $splayer): }?>
<p>
Player: <?php echo htmlspecialchars($splayer['name'], ENT_QUOTES, 'UTF-8'); ?>.<?php echo htmlspecialchars($splayer['ccode'], ENT_QUOTES, 'UTF-8'); ?>
</p>
<?php if (isset($signedup))
{ endforeach; } ?>
McInfo
DevNet Resident
Posts: 1532 Joined: Wed Apr 01, 2009 1:31 pm
Post
by McInfo » Sat Oct 09, 2010 6:14 pm
Invalid:
Code: Select all
if (isset($signedup)) {
foreach ($signedup as $splayer):
}
if (isset($signedup)) {
endforeach;
}
Valid:
Code: Select all
if (isset($signedup)) {
foreach ($signedup as $splayer):
endforeach;
}
Valid:
Code: Select all
if (isset($signedup)) :
foreach ($signedup as $splayer):
endforeach;
endif;
AdrenalineSeed
Forum Newbie
Posts: 24 Joined: Sat Oct 09, 2010 5:54 pm
Post
by AdrenalineSeed » Sat Oct 09, 2010 6:19 pm
But I have HTML code in there? How could I endforeach before I get my HTML code in?
AdrenalineSeed
Forum Newbie
Posts: 24 Joined: Sat Oct 09, 2010 5:54 pm
Post
by AdrenalineSeed » Sat Oct 09, 2010 6:32 pm
This seems to work but now I am echoing every bit of HTML, if the HTML gets really big and complicated, thats going to be messy.
Code: Select all
<?php
if (isset($signedup))
{
foreach ($signedup as $splayer):
echo htmlspecialchars($splayer['name'], ENT_QUOTES, 'UTF-8');
echo ".";
echo htmlspecialchars($splayer['ccode'], ENT_QUOTES, 'UTF-8');
echo "<p></p>";
endforeach;
}
?>
McInfo
DevNet Resident
Posts: 1532 Joined: Wed Apr 01, 2009 1:31 pm
Post
by McInfo » Sat Oct 09, 2010 6:43 pm
Don't echo it, then. Use the same syntax you had before.
The only change I made was in the structure of the if statement. I stripped out the other code because it was irrelevant to the problem.
AdrenalineSeed
Forum Newbie
Posts: 24 Joined: Sat Oct 09, 2010 5:54 pm
Post
by AdrenalineSeed » Sat Oct 09, 2010 6:50 pm
McInfo wrote: Don't echo it, then. Use the same syntax you had before.
The only change I made was in the structure of the if statement. I stripped out the other code because it was irrelevant to the problem.
Oh so I can put a ?> in the middle of an if statement so that I can switch to HTML?
OOOH yea thats what I am doing int he for statement. LOL so strange.... I expect that ?> means stop similar to </script>