Quantcast
Channel: How to properly use for loop when fetching data in mysql and storing it in array then implode it to display in my jquery data table? - Stack Overflow
Viewing all articles
Browse latest Browse all 2

How to properly use for loop when fetching data in mysql and storing it in array then implode it to display in my jquery data table?

$
0
0

need help for my code. What i want is to display my data that looks like this format:Store | Category | Product Name | Description | Price | Timestamp | ActionsSample : Sample cat : Sample Product : Sample Desc : Sample Label - Sample Price : Sample Timestamp : Sample Actions

My query for that is this:$get_product = $mysqli->query("SELECT b.menu_name AS store, a.category AS category, a.product AS product, a.description AS description, a.stamp AS stamp, a.id AS id, a.image AS image FROM lfood a JOIN branch_map b ON a.menu_map = b.id WHERE a.status = 1 ".$searchQuery." ORDER BY ".$columnName." ".$columnSortOrder." LIMIT ".$row." , ".$rowperpage." ");

Then i fetch them like this:

$data = array();while ($row = $sms_subs->fetch_assoc()) {    $menu_id[]      = $row['id'];    $image[]        = $row['image'];    $store[]        = $row['store'];    $category[]     = $row['category'];    $product[]      = $row['product'];    $description[]  = $row['description'];    $stamp[]        = $row['stamp'];}for ($i=0; $i < count($menu_id); $i++) {    $get_price = $mysqli->query("SELECT * FROM lfood_price WHERE menu_id = '".$menu_id[$i]."'");    while ($row_price = $get_price->fetch_assoc()) {        $price[] = $row_price['label'].' : '.number_format($row_price['price']);    }    $new_price = implode("<br>", $price);    $data[] = array("store"         => $store[$i],"category"      => $category[$i],"product"       => $product[$i],"description"   => $description[$i],"price"         => $new_price,"stamp"         => date("F d Y h:i:s A", strtotime($stamp[$i])),"actions"       => "sample"    );}

I have another table that stores all the prices of my product. And the result weren't what it want,This is the actual result

On my second row of data it displays all records of prices even it doesn't belong to that id. My thought of that is the improper use of my for when fetching prices. Please someone help / enlighten the proper use of for loop in regards of my problem. Thank you so much!


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images