PHP Laravel display array as HTML - php

I'm quite new to PHP Blade/Laravel and I'm trying to display an array, which is inside an variable, as HTML.
So I got an variable named $products and first of all I did this:
{{ print_r($products) }}
This gave me:
Array
(
[0] => Array
(
[title] => Belt
[image] => img/products/belt.jpg
[price] => 79.00
[specialPrice] =>
)
[1] => Array
(
[title] => Hat
[image] => img/products/hat.jpg
[price] => 89.00
[specialPrice] => 69.00
)
[2] => Array
(
[title] => Bag
[image] => img/products/bag.jpg
[price] => 99.00
[specialPrice] => 59.00
)
[3] => Array
(
[title] => Scarf
[image] => img/products/scarf.jpg
[price] => 119.00
[specialPrice] =>
)
)
1
So, I want to display this Array as HTML. I tried this:
#foreach($products as $key => $item)
<p>{{#item->title}}</p>
#endforeach
But that didnt work. What am I doing wrong?

Try this :
#foreach($products as $product)
<p>{{$product->title}}</p>
#endforeach
or this
#foreach($products as $product)
<p>{{$product[title]}}</p>
#endforeach

Related

how to Array inside Array Data in laravel

I am trying to get array value in BLADE file. My array is shown below but I am not able to get value inside array.
I have get [student_levels]=> [level] Data
laravel Code
foreach($items as $item)
{
echo $item['student_levels']['level'];
}
$items is Array
Array
(
[0] => Array
(
[_id] => 5c1e4295b0dace71ec62e325
[email] => dhavalpansuriya1195#gmail.com
[franchisecode] => MELHH
[title] => Mr
[firstname] => dhaval s
[lastname] => patel
[gender] => Male
[age] => 16-18
[phone] => 9601354880
[address] => rajkot
[city] => Ahmedabad
[country] => India
[tempcode] => hUmoEF
[studentid] => HTNKLF
[updated_at] => 2019-11-21 13:32:31
[created_at] => 2018-12-22 19:26:37
[student_status] => eda
[student_levels] => Array
(
[_id] => 5c8b11d2b0dace2f9a044d86
[student_id] => HTNKLF
[teacher_id] => 5c1e1ab5b0dace716e429654
[level_name] => recomended_level
[level] => Level 1
[comments] => hhbhhhh
[exam_type] => eda
[student_course] =>
[updated_at] => 2019-03-15 08:15:38
[created_at] => 2019-03-15 08:15:38
)
)
You have an array inside an array..so you can use foreach loop as
#foreach($items as $item)
{{$item['student_levels']['level']}}
#endforeach
Or directly
{{$items[0]['student_levels']['level']}}
It seems your array is returning an array of students, you have an extra level in your array. So to fetch the first student level, you need to use the following code:
echo $item[0]['student_levels']['level'];
Maybe the code that fetches this data could be optimized to only fetch a single student, could you post this code?

search and unset array in array by value in php

My array look like this :
[cart_seller] => Array
(
[3] => Array
(
[หมวด1เลือก1 หมวด2เลือก1 1705] => Array
(
)
)
[4] => Array
(
[# 801] => Array
(
)
)
)
[cart_product] => Array
(
[หมวด1เลือก1 หมวด2เลือก1 1705] => Array
(
[id] => 1705
[name] => ทดสอบสินค้า
[image] => p1534937865-VASAVAT LAB N MEDIA LOGO W.png
[price] => 1111
[option] => หมวด1เลือก1 หมวด2เลือก1
[amount] => 1
)
[# 801] => Array
(
[id] => 801
[name] => โบว์แพรแถบ ร.9 ชนมพรรษา 84 พรรษา ปีพุทธศักราช 2554
[image] => p1498062217-ส.jpg
[price] => 90
[option] =>
[amount] => 1
)
)
I want unset '# 801' in cart_seller and cart_product
in cart_product use unset($cart['cart_product'][# 801]);
but in cart_seller it in array [4] what can i do without reference value (4) ?
exapmle unset($cart['cart_seller'][xxxx][# 801]);
Just loop cart seller array til you find #801.
foreach($cart['cart_seller'] as $key => $c){
if(array_key_exists("#801",$c)){
unset($cart['cart_seller'][$key]['#801']);
}
}

Echo arrays in foreach loop codeigniter

I've been using codeigniter for years but there's a really big gap in between so i always find myself in situations where i forgot how to do things and its almost midnight here so my brain isn't working fast. Can someone show me how to echo the array i have and explain to me how they are processed in the foreach loops?
I have this code in my model to take the rows in 2 tables.
public function tag_genre(){
$result['tag'] = $this->db->get('tags')->result_array();
$result['genre'] = $this->db->get('genre')->result_array();
return $result;
}
And I have this in my controller
public function view_publish_story(){
$data = array('tag_genre' => $this->story_model->tag_genre(), 'title' => "New Story");
$this->load->view('template/header',$data);
$this->load->view('template/navbar');
$this->load->view('pages/storypublish',$data);
$this->load->view('template/footer');
}
I used print_r in my view and this is the result. Seeing this just confuses me more. It's been atleast 2 years since i even dealt with foreach loops.
Array ( [tag] => Array ( [0] => Array ( [tag_id] => 1 [tag_name] =>
LitRPG ) [1] => Array ( [tag_id] => 2 [tag_name] => Virtual Reality )
[2] => Array ( [tag_id] => 3 [tag_name] => Cyberpunk ) [3] => Array (
[tag_id] => 4 [tag_name] => Reincarnation ) [4] => Array ( [tag_id] =>
5 [tag_name] => Summoned Hero ) [5] => Array ( [tag_id] => 6
[tag_name] => Martial Arts ) [6] => Array ( [tag_id] => 7 [tag_name]
=> Slice of Life ) [7] => Array ( [tag_id] => 8 [tag_name] => Overpowered ) [8] => Array ( [tag_id] => 9 [tag_name] => Non-Human )
[9] => Array ( [tag_id] => 10 [tag_name] => Anti-hero ) ) [genre] =>
Array ( [0] => Array ( [genre_id] => 1 [genre_name] => action ) [1] =>
Array ( [genre_id] => 2 [genre_name] => adventure ) [2] => Array (
[genre_id] => 3 [genre_name] => comedy ) [3] => Array ( [genre_id] =>
4 [genre_name] => Drama ) [4] => Array ( [genre_id] => 5 [genre_name]
=> Fantasy ) [5] => Array ( [genre_id] => 6 [genre_name] => Historical ) [6] => Array ( [genre_id] => 7 [genre_name] => Horror ) [7] => Array
( [genre_id] => 8 [genre_name] => Psychological ) [8] => Array (
[genre_id] => 9 [genre_name] => Romance ) [9] => Array ( [genre_id] =>
10 [genre_name] => Sci-fi ) [10] => Array ( [genre_id] => 11
[genre_name] => Mystery ) [11] => Array ( [genre_id] => 12
[genre_name] => Tragedy ) [12] => Array ( [genre_id] => 13
[genre_name] => Short Story ) [13] => Array ( [genre_id] => 14
[genre_name] => Satire ) ) )
I've been looking at various questions regarding arrays from assoc to multidimensional and other stuff. Then i finally visited php manual for foreach loop and i finally was able to echo the array. The problem now is that although it echoes my array it also has an error for each, i can probably fix the error part by declaring it as a defined variable. My question is, is there any other better way? or any improvement on how i made my array to make it cleaner or easier to print?
foreach($tag_genre as $key => $value){
foreach($value as $values){
echo $values['tag_name'];
}
}
UPDATE: i changed the way i made the array.
This is now my model:
public function get_tags(){
$query = $this->db->get('tags')->result_array();
foreach($query as $row){
$result[$row['tag_id']] = $row['tag_name'];}
return $result;
}
public function get_genre(){
$query = $this->db->get('genre')->result_array();
foreach($query as $row){
$result[$row['genre_id']] = $row['genre_name'];}
return $result;
}
and my controller:
public function view_publish_story(){
$data = array('tag' => $this->story_model->get_tags(), 'genre' => $this->story_model->get_genre(), 'title' => "New Story");
$this->load->view('template/header',$data);
$this->load->view('template/navbar');
$this->load->view('pages/storypublish',$data);
$this->load->view('template/footer');
}
and in my view:
<tr>
<div class="form-group">
<td><label for="genre">Genre</label></td>
<?php
foreach($genre as $genre_id => $genre_name){
echo "<td><label class='checkbox-inline'><input type='checkbox' value=''>".$genre_name."</label><td>";
}
?>
</div>
</tr>
My problem now is that, how do i fit all these into my table? I just ruined my table right now since i echoed them all in a single line. How do i do it so that it is printed in 3 columns?
foreach($tag_genre as $key => $value){
foreach($value as $values){
echo '<pre>';
echo $values['tag_name'];
}
}
this will format your array in a way you can read it and understand it.

php delete specific value from array

I have an array $products that looks like this
Array
(
[services] => Array
(
[0] => Array
(
[id] => 1
[icon] => bus.png
[name] => Web Development
[cost] => 500
)
[1] => Array
(
[id] => 4
[icon] => icon.png
[name] => Icon design
[cost] => 300
)
)
)
I am trying to delete the part of array that matches [id] => 1 and for this I am using the following code
$key = array_search('1', $products);
unset($products['services'][$key]);
However it is not working and I am not getting any error either.
What am i doing wrong?
This should work for you:
$key = array_search('1', $products["services"]);
//^^^^^^^^^^^^ See here i search in this array
unset($products['services'][$key]);
print_r($products);
Output:
Array ( [services] => Array ( [1] => Array ( [id] => 4 [icon] => icon.png [name] => Icon design [cost] => 300 ) ) )
And if you want to reindex the array, so that it starts again with 0 you can do this:
$products["services"] = array_values($products["services"]);
Then you get the output:
Array ( [services] => Array ( [0] => Array ( [id] => 4 [icon] => icon.png [name] => Icon design [cost] => 300 ) ) )
//^^^ See here starts again with 0
This will loop through $products['services'] and delete the array whose 'id' key has value 1. array_values just re-indexes the array from 0 again.
foreach($products['services'] as $key => $service)
{
if($product['id'] == 1)
{
unset($products['services'][$key]);
array_values($products['services']);
break;
}
}

how to read a json file containing url and save the url

I have a json file. The json file contains the path of some images. I want to read the url path and save the image in my computer. I was able to read the json file but can't get the image_path object in the json. My json file is of the format:
Array
(
[0] => Array
(
[product] => Array
(
[product_id] => 262
[product_name] => VD0289 CUT OUT BACK DRESS
[product_inventory] => 2
[product_price] => B
[label_name] => 15
[product_status] => A
[images] => Array
(
[0] => Array
(
[image_id] => 935
[image_path] => http://gird.com/images/thumbnails/0/400/500/VD0289_VD0289.jpg
[image_thumbnail] => http://gird.com/images/thumbnails/0/60/60/VD0289_VD0289.jpg
[image_detailed] => http://gird.com/images/detailed/0/VD0289_VD0289.jpg
[image_type] => M
)
[1] => Array
(
[image_id] => 938
[image_path] => http://gird.com/images/thumbnails/0/400/500/VD0289_VD0289_(4).jpg
[image_thumbnail] => http://gird.com/images/thumbnails/0/60/60/VD0289_VD0289_(4).jpg
[image_detailed] => http://gird.com/images/detailed/0/VD0289_VD0289_(4).jpg
[image_type] => A
)
)
[options] => Array
(
)
)
)
[1] => Array
(
[product] => Array
(
[product_id] => 263
[product_name] => Chic Chanel Inspired Dress - Blue
[product_inventory] => 1
[product_price] => O
[label_name] => 9
[product_status] => A
[images] => Array
(
[0] => Array
(
[image_id] => 939
[image_path] => http://gird.com/images/thumbnails/0/400/500/100678-Blue-1.jpg
[image_thumbnail] => http://gird.com/images/thumbnails/0/60/60/100678-Blue-1.jpg
[image_detailed] => http://gird.com/images/detailed/0/100678-Blue-1.jpg
[image_type] => M
)
[1] => Array
(
[image_id] => 942
[image_path] => http://gird.com/images/thumbnails/0/400/500/100678-Blue-4.jpg
[image_thumbnail] => http://gird.com/images/thumbnails/0/60/60/100678-Blue-4.jpg
[image_detailed] => http://gird.com/images/detailed/0/100678-Blue-4.jpg
[image_type] => A
)
..............................
How can I get all the image path and image_detailed from this file?? My php code is:The code is to only get the first image path.
<?php
$file="dw.json";
$json= json_decode(file_get_contents($file),true);
print_r ($json[0]["product"]["images"][0]["image_path"]);
//print_r($json);
?>
Try this out:
foreach($json as $key => $products) {
$product = $products['product'];
foreach($product['images'] as $key => $image) {
$product_images[$product['product_name']][] = $image['image_path'];
}
}
print_r($product_images);
Use a for loop, like this:
foreach ($json[0]["product"]["images"] as $key)
echo $key['image_path'];

Categories