how to use nested array in while loop? - php

I want to put data in array which then put in excel file but it does not work.
$sql="SELECT `Jobc_id`, `Customer_name`, `Veh_reg_no`, `MSI_cat`, `Mileage` FROM `jobcard`";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()){
array( $row["job_id"],$row["Customer_name"],$row["Veh_reg_no"],$row["MSI_cat"],$row["Mileage"]);
}
foreach($rows as $row)
$writer->writeSheetRow('Sheet1', $row);
Whereas, below code work..
$rows = array(
array('2003','1','-50.5','2010-01-01 23:00:00','2012-12-31 23:00:00'),
array('2003','B1', '23.5','2010-01-01 00:00:00','2012-12-31 00:00:00'),
);
foreach($rows as $row)
$writer->writeSheetRow('Sheet1', $row);
How can I make first code to work :(
pls help

you are not assigning the array to a variable, therefore you can't access your fetched data after the while loop. This should fix it:
$rows = [];
while($row = $result->fetch_assoc()){
$rows[] = [$row["job_id"],$row["Customer_name"],$row["Veh_reg_no"],$row["MSI_cat"],$row["Mileage"]];
}
// now you can use $rows

Related

Merge Multiple Array Objects in While Loop then JSON Encode

I'm trying to get, in result, one JSON string from a database of mine but the database returns multiple rows of data, that when JSON encoded, don't fit together with proper formatting.
My current PHP code
$conn = [omitted];
$sql = [omitted];
$result = $conn->query($sql); //this query has been proven to work
if ($result->num_rows > 0) {
$rows = [];
while($row = $result->fetch_assoc()) {
$row[] = array_merge($row,$rows);
}
echo json_encode($rows);
} else {
echo "0";
}
But this code echos the $rows array before it's been merged with the $row arrays.
Current Result:
[]
Wanted Result:
[{"id":"1","team_name":"[omitted]","has_finished":"0"},{"id":"2","team_name":"[omitted]","has_finished":"0"},{"id":"3","team_name":"[omitted]","has_finished":"0"},{"id":"4","team_name":"[omitted]","has_finished":"0"},{"id":"5","team_name":"[omitted]","has_finished":"0"},{"id":"6","team_name":"[omitted]","has_finished":"0"},{"id":"7","team_name":"[omitted]","has_finished":"0"}]
You are never assigning any data into your $rows array. To assign each row into the array you want this for your loop.
while($row = $result->fetch_assoc()) {
$rows[] = $row;
}

Array for a SQL database using PHP

Can someone please help? I'm new to PHP and struggling to make this bit of code to work. For example I have a sql database table with the following schema and data:
Type....rent_price
a..........100
b..........200
c..........300
I want to be able to echo say, "a", in one section and "200" in another. The following code will display "a" but then I can't seem to get it to display anything from the rent_price column using a second array.
$result = $mysqli->query("SELECT * FROM dbc_posts ORDER BY ID ASC limit 3");
for ($set = array (); $row = $result->fetch_assoc(); $set[] = $row['type']);
for ($set1 = array (); $row = $result->fetch_assoc(); $set1[] =$row['rent_price']);
?>
<?php echo $set[0];?>
<?php echo $set1[1];?>
You loop through the results twice, without resetting. Try to loop only once:
$result = $mysqli->query("SELECT * FROM dbc_posts ORDER BY ID ASC limit 3");
$set = array ();
$set1 = array ();
while ($row = $result->fetch_assoc())
{
$set[] = $row['type'];
$set1[] =$row['rent_price'];
}
?>
<?php echo $set[0];?>
<?php echo $set1[1];?>
Depending on what you mean by '"a" in one section and "200" in another', you may be able to forgo creating the intermediate arrays and just print the values from your query as you fetch them. Two cells in a table row, for example:
while ($row = $result->fetch_assoc()) {
echo "<tr><td>$row[type]</td><td>$row[rent_price]</td></tr>";
}
your data is in the first element of array
$set1[0]
but youre probably better off maintaining the naming throughout
$results = array();
while ($row = $result->fetch_assoc()){
$results[] = $row;
}
foreach ($results as $result){
echo $result['type'];
echo $result['rent_price'];
}
OR
$results = array();
while ($row = $result->fetch_assoc()){
$results['types'][] = $row['type'];
$results['rent_prices'][] = $row['rent_price'];
}
foreach ($results['types'] as $type){
echo $type;
}
foreach ($results['rent_prices'] as $rent_price){
echo $rent_price;
}

how to view table value without while loop?

while($row = mysql_fetch_assoc($interst_list_select_result))
it's work one value to many value
I need to work without while loop but i need to all value in table.
how to i work this??
You can use a foreach loop.
$stmt = $db->prepare('SELECT * FROM tbl');
$stmt->execute();
$rows = $stmt->fetchAll();
foreach ($rows as $row => $val){
echo $val['colname1'],' ',$val['colname2'];
}
If you need only the first result I think you can do this way
If( $row = mysql_fetch_assoc($interst_list_select_result)){
echo $row['you_column'];
}
if you need all
$rows = $stmt->fetchAll();
foreach ($rows as $row => $index){
echo $index['you_column'];
}

How to add an entry to mysqli array

I am trying to add a single column at the beginning of a csv file using the code below:
while ($row = mysqli_fetch_array($rows, MYSQL_ASSOC)) {
$list = "'2795', $row";
fputcsv($output, $list);
}
What am I missing? I know it's something simple. Thank you in advance.
You can't just join those values together:
$list = "'2795', $row";
Since $row returns a row result array, treat it as such, push that value inside:
$output = fopen('whatevername.csv', 'a+');
while ($row = mysqli_fetch_array($rows, MYSQLI_ASSOC)) {
$row[] = '2795'; // `$row` is an associative array
fputcsv($output, $row);
}
fclose($output);
Sidenote: This is a truncated code, so just make sure you have that file handle above this code that you presented.

How to insert data to php numeric array

I am trying to inset data to a numeric array php graph.
I tried to do it this way:
$query = "select id,sales from riders";
$results = mysql_query($query);
while($row = mysql_fetch_array($results)) {
//$rider[i]=$row['id'];
$rider[$counter]=$row['sales'];
$counter++;
}
and insert into data array as follows:
$data = array($rider[0],$rider[1],$rider[2],$rider[3],$rider[4]);
It gives me error.. Please help me... Thank you very much for your kind help.....
Add $counter = 0; before the while loop.
PS: You can just use $data[] = $row['sales']; in the loop without the $counter.
Use $rider[],
$rider=array();
while($row = mysql_fetch_array($results))
{
$rider[]=$row['sales'];
}

Categories