-1

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?

azgold816
  • 1
  • 3
  • have u tried var_dump($row)?? what it shows?? also ur are doing while($row=mysqli_fetch_array()) instead try mysqli_fetch_assoc – RohitS Sep 20 '17 at 17:11
  • 1
    Note :also your code is pron to sql injection use pdos or filter data before you inject in query – RohitS Sep 20 '17 at 17:14
  • var_dump shows array(25) { ["ID"]=> string(1) "2" ["Task1"]=> string(35) "CHECK ALL ELECTRICAL WIRES FOR WEAR" ["Task2"]=> string(46) "CHECK QUILL WHEELS FOR WEAR, REPLACE AS NEEDED" ["Task3"]=> string(34) "CHECK MOTOR AND GEARBOX FOR DAMAGE" ["Task4"]=> string(61) "CHECK MACHINERY IS WORKING PROPERLY/CONTROLS WORKING PROPERLY" ["Task5"]=> string(47) "CHECK GRIPPER BELTS FOR WEAR, REPLACE AS NEEDED" ["Task6"]=> string(62) "CHECK ALL DOORS AND COVERING FOR DAMAGES, REPLACE AS NECESSARY" ["Task7"]=> string(0) "" ["Task8"]=> string(0) "" ["Task9"]=> string(0) "" – azgold816 Sep 20 '17 at 17:18
  • as you can see it already fetched all records... if you want to iterate use mysqli_fetch_assoc() and then go ahead or iterate in foreach loop for previous solution – RohitS Sep 20 '17 at 17:19
  • however it does not show the value in my page of task1 ect. just the yes no buttons. I'm assuming its the echo $row['Task$Task']; isn't formatted correctly to display it. as well its not looping thru each task number – azgold816 Sep 20 '17 at 17:20
  • [Little Bobby](http://bobby-tables.com/) says ***[your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)*** Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php). Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard Sep 20 '17 at 17:23
  • Please do not dump code in comments. – Jay Blanchard Sep 20 '17 at 17:23
  • 1
    nope it should be concatenation so $row['Task'.$Task]; – RohitS Sep 20 '17 at 17:24
  • FYI: variables don't parse in single quotes. – Funk Forty Niner Sep 20 '17 at 17:24

2 Answers2

0

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

    echo $row["Task$Task"];

Or

  echo $row['Task'.$Task];
Niklesh Raut
  • 34,013
  • 16
  • 75
  • 109
-1

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();
?>
RohitS
  • 1,036
  • 12
  • 17