Raiyu
Raiyu

Reputation: 101

Array inserting only one row

I'm trying to export my database in excel using php. but the while loop is only inserting one row of values in the array and exported excel file will only have one only row of data.

I'm using a nested loop for table callback to insert it's data if it's not empty that is. So, basically there is two databases of values being exported to one excel file. First the data is is being stored in an array and then it is exported as an excel file.

Here's the code :

   $sql = "SELECT * FROM leads";
   $result = mysqli_query($conn, $sql);
   $data = array();
    while ($row1 = mysqli_fetch_array($result))
    {
    if ($row1["status"] == "1") 
    {
    $status = "New";
    }
    if ($row1["status"] == "2") 
    {
    $status = "Assigned";
    }
    if ($row1["status"] == "3") 
    {
    $status = "Pending";
    }
    if ($row1["status"] == "4") 
    {
    $status = "Closed";
    }
    if ($row1["status"] == "5") 
    {
    $status = "Denied";
    }
    $id = $row1["id"];
    $sql2 = "SELECT * FROM callback WHERE lead_id=$id ORDER BY id DESC LIMIT 1";
   $result2 = mysqli_query($conn, $sql2);
    if ($result2)
    {
    while ($row2 = mysqli_fetch_array($result2))
    {


    $data[] = array(
    "Created" => $row1["curdate"], 
    "Customer Name" => $row1["cname"], 
    "Designation" => $row1["designation"],
    "Contact" => $row1["number"],
    "Company" => $row1["company"],
    "Address" => $row1["street"],
    "Landmark" => $row1["landmark"],
    "Zip" => $row1["zip"],
    "Products" => $row1["product"],
    "Last Callback" => $row2["callback"],
    "Last Visit" => $row2["vdate"],
    "Status" => $status
   );
   }
   }
   else
   {
    $data[] = array(
   "Created" => $row1["curdate"], 
    "Customer Name" => $row1["cname"], 
    "Designation" => $row1["designation"],
    "Contact" => $row1["number"],
    "Company" => $row1["company"],
    "Address" => $row1["street"],
    "Landmark" => $row1["landmark"],
    "Zip" => $row1["zip"],
    "Products" => $row1["product"],
    "Last Callback" => "NULL",
    "Last Visit" => "NULL",
    "Status" => $status
    );
    }
    }

What am i doing wrong? Also i'm a noob at this so, please go easy on me thanks.

Upvotes: 0

Views: 55

Answers (1)

Nick
Nick

Reputation: 147146

$result2 is a mysqli_result object, you can't use it in an if statement. You should use its num_rows property instead i.e.

if ($result2->num_rows > 0) 

(in place of if ($result2))

Upvotes: 1

Related Questions