-1

I have a simple challenge here with php mysql . I am a newbee in php and trying to write some code .

Below is my code:

$checked1 = $row['rmc_induction_softskill'];
$checked2= $row['rmc_induction_neo'];
$checked3 = $row['rmc_induction_itil'];
$checked4 = $row['rmc_induction_hr_induction'];
$checked5 = $row['rmc_induction_vmware'];
$checked6 = $row['rmc_induction_lss'];

I wish to run a loop and run single html line with all the above variable name in it in one-by-one fashion . For example , i want to have $checked1 == 1 , $checked2 == 1 .I have tried below code but not able to get $checked1 each time in loop

for ($i=1; $i<=33; $i++){
$checked = checked{$i};
echo $checked;
<td align="center" bgcolor="#E6E6FA">  <input type="checkbox" id="<?php echo $i.$rows['emp_id']; ?>" name="Emails[]" value="<?php echo $i.";".$concatenated_string; ?>" <?php if ($checked == 1) echo "checked" ; ?>  onclick="check(<?php echo $i.$rows['emp_id']; ?>);"  /></td>
2
  • 2
    This code has several syntax errors that make it difficult to deduce the desired HTML output. Can you post an example of the expected output? Commented Jun 19, 2013 at 19:53
  • stackoverflow.com/a/433319/1053820 Should help Commented Jun 19, 2013 at 19:59

5 Answers 5

2

You can just assign a variable with the name of your var:

$check0 = 1;
$check1 = 0;
$check2 = 1;
$check3 = 1;

for($i = 0; $i < 4; $i++) {
    $var = "check$i";
    echo ($$var == 1 ? 'true' : 'false') . '<br />';
}

I hope this is what you're trying to do.

Sign up to request clarification or add additional context in comments.

2 Comments

i want to have $checked1=1 , $checked2=1 ,@checked=3 . I want that $checked.$i to be random
please explain in other words. with this code, you can use it with $var = 'checked'.$i. it's the same as "checked$i".
0

You can't use checked{$i} to simulate checked1 or checked2 or ... What you have to do is to put/push everything into an array with key value pair and then recover them based on the index of the list. I believe your MySQL read returns an array of key value pair so you can loop through that, but if you want to choose the specific fields, then you can create an array in PHP and push the items in it. Let me give you an example:

// insert into the checked array. Insert all those fields/rows that you want 
$checked = array($row['rmc_induction_softskill'], $row['rmc_induction_neo'], $row['rmc_induction_itil']);

for ($i=1; $i<=count($checked); $i++){
$checkedTemp = checked[$i];
echo $checkedTemp;
<td align="center" bgcolor="#E6E6FA">  <input type="checkbox" id="<?php echo   $i.$rows['emp_id']; ?>" name="Emails[]" value="<?php echo $i.";".$concatenated_string; ?>" <?php if ($checkedTemp == 1) echo "checked" ; ?>  onclick="check(<?php echo $i.$rows['emp_id']; ?>);"  /></td>

So that is a way of doing it. Based on the order you insert into the array you can take it out too.

1 Comment

You are the boss !!! .. You did my work so easy . Thanks a lot . But i get below output when tried your way . I need another $ to the content .checked1 checked2 checked3 checked4 checked5 checked6 checked7 checked8 checked9 checked10 checked11 checked12 checked13 checked14 checked15 checked16 checked17 checked18 checked19 checked20 checked21 checked22 checked23 checked24 checked25 checked26 checked27 checked28 checked29 checked30 checked31 checked32
0

I'm having trouble understanding exactly what it is you're trying to do, but here's a generic suggestion for what you might be able to do...

$checked[1] = $row['rmc_induction_softskill']
$checked[2] = $row['rmc_induction_neo']
....etc

for ($i=0;$i<sizeof($checked);$i++) {
    echo $checked[$i];
}

Don't know if this is what you're looking for, so let me know if you need something more specific and add some extra information so I know a bit better what you're trying to do.

Comments

0

That's the closest i can just assume you want

http://3v4l.org/mV0TO

<?php

    $rows['emp_id'] = 333;
    $concatenated_string = "hello";

    $checked1 = 1;// $row['rmc_induction_softskill'];
    $checked2= 1;//$row['rmc_induction_neo'];
    $checked3 = 0;//$row['rmc_induction_itil'];
    $checked4 = 1;//$row['rmc_induction_hr_induction'];
    $checked5 = 0;//$row['rmc_induction_vmware'];
    $checked6 = 0;//$row['rmc_induction_lss'];

    for ($i=1; $i<=6; $i++)
    {
        $checked = ${'checked'.$i};
        echo $checked;

        echo '
<td align="center" bgcolor="#E6E6FA">
    <input type="checkbox" id="'.$i.$rows['emp_id'].'" name="Emails[]" value="'.$i.';'.$concatenated_string.'" '.($checked == 1)?'checked':''.'  onclick="check('.$i.$rows['emp_id'].');"  />
</td>';

     }
?>

1 Comment

Thanks to all for helping me on this . I appreciate your time and response. Alex has resolved my challenge . Special thanks to him .Below is the tweaked version of it . for ($i=0; $i<=count($checked); $i++){ $checkedTemp = $checked[$i]; echo $checkedTemp;
0

Below is the answer for my question . Help by Alex :

Put the variables or data from database into a array .as shown below .

$checked = array($row['rmc_induction_softskill'], $row['rmc_induction_neo'], $row['rmc_induction_itil']);

Put looping on the array created , use count of array to get the total number .

for ($i=0; $i<=count($checked); $i++){
$checkedTemp = $checked[$i];
echo $checkedTemp;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.