Having a issue with php statement using loop mysql

2019-03-04 19:06发布

问题:

Hello Please forgive me if I'm not asking this right. I have the following code.

      <?php

      //Connect to mysql server
    include ("Data.php");
    if (!$con) {
    die ("connection error". mysqli_connect_error());
    }

$sql1 = "SELECT * FROM PMList where AssetNum= '$AssetNum' and Plant= '$Plant';";
    $result = mysqli_query($con, $sql1) or die(mysqli_error($con));

if ($result->num_rows > 0)
$count = 0;
$Task = 1;
while($row = mysqli_fetch_array($result))
{
            $Task++;
            echo "<td bgcolor='#D8D8D8' align='Left'>";
            echo "<font size='2'>";
            echo $row['Task$Task'];
            echo " </font></td>";
            echo "<td bgcolor='#D8D8D8'><input type='radio' name='Task$Task' value='Yes'>Yes";
            echo "&nbsp;&nbsp;<input type='radio' name='Task$Task' value='No'>No";
            echo "<tr border='0'>";
            }
    while ($count++ < 16) {
}
        $con->close();
?>

What I am trying to do is add the $task value to the echo $row['Task$Task']; So that the value turns to task1 then task2 , task3 ect. There can be up to 15 tasks. I'm close just not sure on where I'm messing up. Any help would be great. been stuck on this one for awhile now. Thank you in advance!!


OK now this is what I have its displaying almost correctly.

$sql1 = "SELECT * FROM PMList where AssetNum= '$AssetNum' and Plant= '$Plant';";
    $result = mysqli_query($con, $sql1) or die(mysqli_error($con));

if ($result->num_rows > 0)
$count = 0;
$Task = 1;

while($row = mysqli_fetch_assoc($result))
do
{
            echo "<td bgcolor='#D8D8D8' align='Left'>";
            echo "<font size='2'>";
            echo $row['Task'.$Task];
            echo " </font></td>";
            echo "<td bgcolor='#D8D8D8'><input type='radio' name='Task$Task' value='Yes'>Yes";
            echo "&nbsp;&nbsp;<input type='radio' name='Task$Task' value='No'>No";
            echo "<tr border='0'>";
            $Task++;
            }
    while ($count++ <= 13);
        $con->close();
?>

Now it is looping however, If the task is empty I need to not echo the radio buttons and stop where it ends. capture. Thank you guys your all awesome!! How do I add a if statement thst can see if the $row['Task'.$Task]; is null then stop the loop?

回答1:

You should use " to make string with variable or use . dot to concatenation , either

    echo $row["Task$Task"];

Or

  echo $row['Task'.$Task];


回答2:

Note(This is just for example and prone to sql injection you should check how to Use PDO's and Sanitize php input

<?php

//Connect to mysql server
include ("Data.php");
if (!$con) {
die ("connection error". mysqli_connect_error());
}

$sql1 = "SELECT * FROM PMList where AssetNum= '$AssetNum' and Plant= '$Plant';";
    $result = mysqli_query($con, $sql1) or die(mysqli_error($con));

if ($result->num_rows > 0)
$count = 0;
$Task = 1;
$rows = mysqli_fetch_array($result)
foreach($rows as $row )
{
            $Task++;
            echo "<td bgcolor='#D8D8D8' align='Left'>";
            echo "<font size='2'>";
            echo $row["Task".$row["id"]];
            echo " </font></td>";
            echo "<td bgcolor='#D8D8D8'><input type='radio' name='$row["Task".$row["id"]]' value='Yes'>Yes";
            echo "&nbsp;&nbsp;<input type='radio' name='$row["Task".$row["id"]]' value='No'>No";
            echo "<tr border='0'>";
            }
        $con->close();
?>

or

<?php

//Connect to mysql server
include ("Data.php");
if (!$con) {
die ("connection error". mysqli_connect_error());
}

$sql1 = "SELECT * FROM PMList where AssetNum= '$AssetNum' and Plant= '$Plant';";
    $result = mysqli_query($con, $sql1) or die(mysqli_error($con));

if ($result->num_rows > 0)
$count = 0;
$Task = 1;
while($row = mysqli_fetch_assoc($result))
{
            $Task++;
            echo "<td bgcolor='#D8D8D8' align='Left'>";
            echo "<font size='2'>";
            echo $row["Task".$row["id"]];
            echo " </font></td>";
            echo "<td bgcolor='#D8D8D8'><input type='radio' name='$row["Task".$row["id"]]' value='Yes'>Yes";
            echo "&nbsp;&nbsp;<input type='radio' name='$row["Task".$row["id"]]' value='No'>No";
            echo "<tr border='0'>";
            }
        $con->close();
?>


标签: php mysqli