From what I've read, it isn't possible to echo more PHP code within an echo so I'm after a solution to the following:
Basically, if the 'closed_state' column in my DB equals 'yes', display a link with a variable on the end else display a different link with a variable on the end.
Here's my code:
<?php
$result1 = mysqli_query($con,"SELECT closed_state FROM tbl_company WHERE company_id='$company_id'") or die(mysql_error());
$result2 = mysqli_query($con,"SELECT company_id FROM tbl_company WHERE company_id='$company_id'") or die(mysql_error());
$result3 = mysqli_query($con,"SELECT company_id FROM tbl_company WHERE company_id='$company_id'") or die(mysql_error());
while($row = mysqli_fetch_array($result1))
{
if ($row['closed_state'] == "yes")
{
echo "<a href="customers_open.php?company_id=<?php while($row = mysqli_fetch_array($result2)) echo "{$row['company_id']}"; ?>">Reopen account</a>";
} else {
echo "<a href="customers_close.php?company_id=<?php while($row = mysqli_fetch_array($result3)) echo "{$row['company_id']}"; ?>">Close account</a>";
}
}
?>
Obviously it doesn't work so what's the best way to achieve this?
I don't think you need 3 queries. One should be fine selecting both values. Queries 2 & 3 are identical anyway.
$result1 = mysqli_query($con,"SELECT company_id, closed_state FROM tbl_company WHERE company_id='$company_id'") or die(mysql_error());
while($row = mysqli_fetch_array($result1)){
if ($row['closed_state'] == "yes"){
echo '<a href="customers_open.php?company_id='.$row['company_id'].'">Reopen account</a>';
}else{
echo '<a href="customers_close.php?company_id='.$row['company_id'].'">Close account</a>';
}
}
PHP isn't recursively embeddable, or executable. You cannot embed php code in a php string and have it executed automatically, e.g.
<?php
$foo = "This is a <?php echo 'string'; ?> in PHP";
echo $foo;
will give you the literal output:
This is a <?php echo 'string'; ?> in PHP
instead of the
This is a string in PHP
that you want it to.
If you want to "embed" php like that, just build a string:
$foo = "This is a " . 'string' . " in PHP";
You are not properly quoting your strings. If you have the string echo "foo"bar"
then then middle double-quote will cause PHP to end the string and try to parse bar
as something else.
Likewise, you cannot nest PHP inside of PHP.
echo '<a href=....<?php while($row...
I'm going to assume that you're second and third queries are actually different, otherwise there is no point in having duplicates - but here is a working version.
<?php
$result1 = mysqli_query($con,"SELECT closed_state FROM tbl_company WHERE company_id='$company_id'") or die(mysql_error());
$result2 = mysqli_query($con,"SELECT company_id FROM tbl_company WHERE company_id='$company_id'") or die(mysql_error());
$result3 = mysqli_query($con,"SELECT company_id FROM tbl_company WHERE company_id='$company_id'") or die(mysql_error());
while($row = mysqli_fetch_array($result1))
{
if ($row['closed_state'] == "yes")
{
$company_id = null;
while($row = mysqli_fetch_array($result2)) $company_id = $row['company_id'];
echo "<a href='customers_open.php?company_id={$company_id}'>Reopen account</a>";
} else {
$company_id = null;
while($row = mysqli_fetch_array($result3)) $company_id = $row['company_id'];
echo "<a href='customers_close.php?company_id={$company_id}'>Close account</a>";
}
}
?>