Menu
Hi,
i have this code
[code=php]$fn = ‘testfunction’;
function testfunction($argument) { whatever; }
$string = <<< heredoc
plain text and now a function: {$fn(‘someargument’)}
heredoc;
as far as i know, that should make it but it wont work, i get the following error:
[QUOTE]
Parse error: syntax error, unexpected ‘(‘, expecting ‘}’ in /home3/tokyoint/public_html/asakusa.htm on line 82
why is that? what am i doing wrong? its possible to have a function executed inside heredoc, isnt?
Thank you.
[code=php]
$result = testfunction('someargument');
$string = <<<EOD
plain text and now a function result: $result
some more text
EOD;
[/code]
[code=php]
$string = sprintf(<<<EOD
plain text and now a function result: %s
some more text
EOD
,
testfunction('someargument')
);
[/code]
[code=php]function getstatus($room)
{
$query = mysql_query("SELECT status FROM rooms WHERE room='$room'");
$stat = mysql_fetch_array($query);
if ($stat[0] == 1)
{
$status ="<B>Available!</B>";
}
else
{
$status ="occupied";
}
return $status;
}[/code]
[code=php]
<?php
function getStatusAll()
{
$status = array();
$query = mysql_query("SELECT room, status FROM rooms");
while ($row = mysql_fetch_assoc($query)) {
$status[$row['room']] = ($row['status'] == 1)
? "<B>Available!</B>"
: "occupied";
}
return $status;
}
$availability = getStatusAll();
foreach($availability as $room => $status) {
echo <<<EOD
Blah blah blah.
Room: $room
Blah blah blah
Availability: $status
Blah blah blah.
EOD;
}
[/code]
[code=html]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<HEAD><?php $lang="en"; ?>
[...a lot of html code...]
<TD class="roomtable">201</TD>
<TD class="roomtable">Middle size</TD>
<TD class="roomtable">59,000</TD>
<TD class="roomtable">$status_room201</TD>
[...more html code...]
<TD class="roomtable">202</TD>
<TD class="roomtable">big size</TD>
<TD class="roomtable">59,000</TD>
<TD class="roomtable">$status_room202</TD>
[...and so on...][/code]
[code=php]
<?php
$status = array();
$query = mysql_query("SELECT room, status FROM rooms");
while ($row = mysql_fetch_assoc($query)) {
$status[$row['room']] = ($row['status'] == 1)
? "<B>Available!</B>"
: "occupied";
}
?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head><title>Example</title></head>
<body>
<p>This is a bunch of static HTML text.</p>
<p>Now we're about to the point where we need the status for room #123.</p>
<p>The status for Room #123 is: <b><?php echo $status[123]; ?></b>.</p>
<p>This is a bunch of static HTML text.</p>
<p>Now we're about to the point where we need the status for room #456.</p>
<p>The status for Room #456 is: <b><?php echo $status[456]; ?></b>.</p>
<p>Okay, that's enough boring stuff for now.</p>
</body>
</html>
[/code]
I tested the code now and it works beautifully!! thank you! i just would like to ask you a few things so i can understand better how you did it. How does mysql_fetch_assoc work? as far as i understand, it associates the 2 fields, but i got lost after that.
$status[$row['room']] = ($row['status'] shouldnt mean that both values are the same? also, i didnt know you could use '?' to replace the 'if' statement and ':' to replace 'else'. That was very informative, i really appreciate it.[/QUOTE]
[code=php]
$row['room'] = 123;
$row['status'] = 1;
$status[$row['room']] = ($row['status'] == 1) ? 'Available' : 'Occupied';
[/code]
...is in effect doing...[code=php]
$status[123] = 'Available';
[/code]
[code=php]$status[$row['room']] = )($row['status'] == 1 ? 'Available' : 'Occupied'; [/code]
[code=php]if($row['status'] == 1) { $status='Available' } else { $status='Occupied'}; [/code]
[code=php]
if($row['status'] == 1) { $status='Available' } elseif ($row['status'] == 2) { $status='reserved'} elseif ($row['status'] == 3) { $status='whatever'} else { $status='Occupied'};
[/code]
[code=php]
while($row = mysql_fetch_assoc($result)) {
switch($row['status']) {
case 1:
$text = 'available';
break;
case 2:
$text = 'reserved';
break;
case 3:
$text = 'whatever';
break;
default:
$text = 'occupied';
}
$status[$row['room']] = $text;
}
[/code]
0.1.9 — BETA 5.4