Having a issue with php statement using loop mysqli

狂风中的少年 提交于 2019-12-31 05:18: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();
?>


来源:https://stackoverflow.com/questions/46328010/having-a-issue-with-php-statement-using-loop-mysqli

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!