0
while($row = mysql_fetch_assoc($sql1))
$sqldiag=$conn->query("select di_name from assi_diagnosis where di_id IN ($disp1$j)");
$i == "0";
$items = array();   
while($rsdiag=mysql_fetch_array($sqldiag))
{
echo $rsdiag['di_name'].">>";
$items[$i] = $rsdiag['di_name'];
print_r($items);
}
$i++;

Friends I would like to save values in array when processing an while loop . my expectation is when the first loop condition is executed $item[0] should contain all the resultset values of second while loop iteration for ex : (

$item[0] = {value1,value2,value3};
$item[1] = {value1,value2,value3};
$item[2] = {value1,value2,value3};

)

$item[0] is the value of first iteration values with in $item[0] are values of second while loop

2
  • 1
    Your first loop does't have opening / closing brackets and what is $i == "0"; supposed to do? It is a conditional statement yet it's not inside an if. Commented Nov 26, 2015 at 15:18
  • You must change the logic of your script.. It is totally wrong.. Commented Nov 26, 2015 at 15:27

3 Answers 3

1

Keep $i++ inside loop.

while($rsdiag=mysql_fetch_array($sqldiag))
{
    echo $rsdiag['di_name'].">>";
    $items[$i] = $rsdiag['di_name'];
   $i++;// this line
}
print_r($items);// print here
Sign up to request clarification or add additional context in comments.

Comments

0
$i = 0;
$items = array();
while ($row = mysql_fetch_assoc($sql1)) {
    $items[$i] = array();
    $sqldiag = $conn->query("select di_name from assi_diagnosis where di_id IN ($disp1$j)");
    while($rsdiag=mysql_fetch_array($sqldiag))
    {
        echo $rsdiag['di_name'].">>";
        array_push($items[$i], $rsdiag['di_name']);
        print_r($items);
    }
    $i = $i + 1;
}
print_r($items);

There were a couple of errors in your code.

  • first while loop didn't have an opening nor closing bracket,
  • you assigned value to $i using conditional operator == instead of =,
  • each time second loop was ran it would override $items[$i] value instead of pushing to it as an array, use array_push, use if for all the $rsdiag values you want to assign,

Comments

0

You have a couple of little problems with your code which are fixed here

<?php
while($row = mysql_fetch_assoc($sql1)) {    
    $sqldiag=$conn->query("select di_name from assi_diagnosis where di_id IN ($disp1$j)");    
    $items = array();
    $i = 0;
    while($rsdiag=mysql_fetch_array($sqldiag))
    {
        echo $rsdiag['di_name'].">>";
        $items[$i] = $rsdiag['di_name'];
        print_r($items);
        $i++;
    }
}

issues:

$i == "0";

should be $i = 0; also

while($row = mysql_fetch_assoc($sql1))

missing {

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.