So I need to display items that were added on the Vend Page and are now in the cart array over onto the cart page in a list
Array
(
[cart] => Array
(
[BRB] => 1
)
)
Here is my code for the cart page that I'm working on:
if (isset($_SESSION['cart'])) {
foreach ($vend as $vendID => $items) {
//if (array_search($vendID, $_SESSION['cart'])) {
echo "<article class ='cart' id='cart-$vendID'>";
echo "<h1 class = 'item-h1' id = 'h1'>{$items['title']}</h1>";
echo "<div class ='item-no'>";
echo "<p class = 'pro-id'><b>Product ID: </b>{$vendID}</p></div>";
echo "<div class ='img-div'>";
echo "<img src =../images/{$items['img']} alt='' height='196' width='200'></div>";
echo "<div class='pricing'>";
echo "<p><b>Price: $</b>{$items['price']}</p></div>";
echo "</article>";
//}
}
}
I commented out code that I attempted but didn't work. So I need an IF statement which says, If the vendID is in the cart array display it in the list.
Some help is appreciated here as I'm stuck.
Your cart should be as follows:
$cart = array(
[0] => Array
(
[item_id] => 1,
[title] => "your title",
[price] => 100,
[img] => "name of the file",
),
[1] => Array
(
[item_id] => 2,
[title] => "your title",
[price] => 200,
[img] => "name of the file",
)
);
And Then Try as follows:
if (isset($_SESSION['cart'])) {
foreach ($cart as $key => $items) { //$key = [0], [1] ....
echo "<article class ='cart' id='cart-$key'>";
echo "<h1 class = 'item-h1' id = 'h1'>".$items['title']."</h1>";
echo "<div class ='item-no'>";
echo "<p class = 'pro-id'><b>Product ID: </b>".$items['item_id']."</p></div>";
echo "<div class ='img-div'>";
echo "<img src =../images/".$items['img']." alt='' height='196' width='200'></div>";
echo "<div class='pricing'>";
echo "<p><b>Price: $</b>".$items['price']."</p></div>";
echo "</article>";
}
}
Mark, it accepted if it works.
Did you try in_array() ?
It checks if a value exists in an array.
Related
Having Issues with nested arrays
I want to get the array title into the first cell of the table, having an issue articulated the nested foreach.
$content = file_get_contents("https://whattomine.com/asic.json");
$data = json_decode($content);
echo "coins";
foreach()
{
echo "<tr>";
echo "<th>".$coins->coinname."</th>";
echo "<th>".$coins->PoW."</th>";
echo "<th>".$coins->PoS."</th>";
echo "<th>".$coins->height."</th>";
echo "<th>".$coins->diff."</th>";
echo "<th>".$coins->supply."</th>";
echo "</tr>";
}
I've looked into the data and think I found what you want.
BUT you try to get values that are not in the data set.
The structure of that json is:
{"coins":
{"LitecoinCash":
{"id":231,"tag":"LCC",...}
},
...
}
So here's how to get that data:
$data = json_decode($content);
#var_dump($data);
echo "<table>";
foreach($data->coins as $title => $coin)
{
echo $title;
echo "<tr>";
echo "<th>".$title."</th>"; // the title as first cell
echo "<th>".$coin->id."</th>"; // added by me, coinname does not exist
echo "<th>".$coin->tag."</th>"; // added by me, PoW does not exist
#echo "<th>".$coin->PoS."</th>"; // all the others do not exist in the dataset.
#echo "<th>".$coin->height."</th>";
#echo "<th>".$coin->diff."</th>";
#echo "<th>".$coin->supply."</th>";
echo "</tr>";
}
echo "</table>";
// output:
// LitecoinCash 231 LCC
There is not keys you provided, but there is next keys:
example of item:
[LitecoinCash] => [
[id] => 231
[tag] => LCC
[algorithm] => SHA-256
[block_time] => 156.0
[block_reward] => 250
[block_reward24] => 250
[last_block] => 1460137
[difficulty] => 470292855.107
[difficulty24] => 396924068.28117
[nethash] => 12948028411711743
[exchange_rate] => 4.1E-6
[exchange_rate24] => 2.9135097493036E-6
[exchange_rate_vol] => 0.34130528
[exchange_rate_curr] => BTC
[market_cap] => $18,382,732.11
[estimated_rewards] => 149.54924
[estimated_rewards24] => 177.15706
[btc_revenue] => 0.00061315
[btc_revenue24] => 0.00072634
[profitability] => 102
[profitability24] => 121
[lagging] =>
[timestamp] => 1533048969
]
Which of them do you need?
I'm trying to get the comment information from a JPG. My example below returns ?
Because it's grabbing the wrong comment.
[WINXP] => Array ( [Comments] => ????????????????????????????????? )
While I need it to grab specifically,
[IFD0] => Array ( [Exif_IFD_Pointer] => 2110 [Comments]
Code:
<?php
$exif_data = exif_read_data('28058990_1835355009821464_5937004451156759325_n.jpg', 0, true);
echo $exif_data===false ? "No header data found.<br />\n" : "Image contains headers<br />\n";
echo $exif_data['Comments'];
print_r($exif_data);
?>
I ended up doing it this way..
<?php
$exif_data=exif_read_data('28058990_1835355009821464_5937004451156759325_n.jpg', 0, true);
$values=array();
foreach($exif_data as $exif => $key)
{
foreach($key as $value)
{
$items[] = $value;
}
}
echo '<img src='.$items[0].' width="256px"><br />'.$items[12];
?>
How to limit inserting data into MySql using codeiginter?
I have 10000 array data.
But I want to limit only 200 data at each process.
Next process have to insert from data that is not yet inserted.
Here is my code from controller:
<?php
foreach($data["result"] as $key) {
$insert=$this->db->insert('master_mahasiswa_pt',$key );
if (!$insert && $this->db->error()) {
echo "Data nim"; echo " "; echo $key['nipd'] ; echo " "; echo "Sudah Ada"; echo '<br>';
} else {
echo "Data nim"; echo " "; echo $key['nipd'] ; echo " "; echo "Sudah Masuk"; echo '<br>';
}
}
}
}
?>
Here is example from my array data:
Array
(
[error_code] => 0
[error_desc] =>
[result] => Array
(
[0] => Array
(
[id_reg_pd] => 35e86b4f-1b8b-407b-b49e-9d125cba2cb5
[nipd] => 150015020
[id_pd] => b5261e3d-6aed-41e9-a329-1ebe6191d53c
[nm_pd] => ACDB
[tgl_lahir] => 1996-06-16
[id_sms] => 63d9ecdb-4468-4883-a1aa-13ad3f88fce1
[fk__sms] => Teknik Informatika
[id_sp] => 89a93cb2-f959-45d3-8a75-f349be529b87
[fk__sp] => STMIK
[tgl_masuk_sp] => 2015-08-25
[id_jns_daftar] => 1
[fk__jns_daftar] => Peserta didik baru
[id_jns_keluar] =>
[fk__jns_keluar] =>
[tgl_keluar] =>
[ket] =>
[skhun] =>
[a_pernah_paud] => 0
[a_pernah_tk] => 0
[mulai_smt] => 20151
[sks_diakui] => 0
[jalur_skripsi] =>
[judul_skripsi] =>
[bln_awal_bimbingan] =>
[bln_akhir_bimbingan] =>
[sk_yudisium] =>
[tgl_sk_yudisium] =>
[ipk] =>
[no_seri_ijazah] =>
[sert_prof] =>
[a_pindah_mhs_asing] =>
[nm_pt_asal] =>
[nm_prodi_asal] =>
)
)
)
$i = 0;
foreach ($data["result"] as $key) {
if ($i < 201) {
$insert = $this->db->insert('master_mahasiswa_pt', $key);
if (!$insert && $this->db->error()) {
echo "Data nim";
echo " ";
echo $key['nipd'];
echo " ";
echo "Sudah Ada";
echo '<br>';
} else {
echo "Data nim";
echo " ";
echo $key['nipd'];
echo " ";
echo "Sudah Masuk";
echo '<br>';
}
}
$i++;
}
I do have an array something like below:
[images] => Array
(
[0] => Array
(
[id] => 1
[path] => ../images/properties/1/1447053991.jpg
)
[1] => Array
(
[id] => 3
[path] => ../images/properties/1/1447054231.jpg
)
[2] => Array
(
[id] => 4
[path] => ../images/properties/1/1447054666.jpg
)
[3] => Array
(
[id] => 17
[path] => ../images/properties/1/1447141341.jpg
)
)
When I need to echo this array, I want to add different HTML for first key of this array and also different HTML for other keys.
This is how I tried it:
foreach($property['images'] as $image) {
//echo '<pre>',print_r($image).'</pre>';
if ($image['id'] != '') {
$html .= " <a href='".$image['path']."' class='image-wrap' title='' rel='prettyPhoto'>\n";
$html .= " <img src='".$image['path']."' alt=''/>\n";
$html .= " <span class='zoom-icon'></span>\n";
$html .= " </a>\n";
} else {
$html .= " <a href='".$image['path']."' title='1' rel='prettyPhoto[group]'></a>\n";
}
}
But its not working for me. Hope somebody may help me out.
If I am not wrong your foreach is printing values as expected and you want the first index to print out something different then others..
foreach($property['images'] as $index => $image) {
//echo '<pre>',print_r($image).'</pre>';
if ($index == 0) {
// do stuff for the first index
} else {
// do stuff for other indexes
}
}
since you want to display first image as image and the rest in anchor tag try this.use a counter to identify first and the rest.there can be two cases.
case 1: when your index does not start with 0.
$property=array('images' => Array
(
'0' => Array
(
'id' => 1,
'path' => '../images/properties/1/1447053991.jpg'
),
'1' => Array
(
'id' => 3,
'path' =>'../images/properties/1/1447054231.jpg'
)
)
);
$html="";
$counter=0;
foreach($property['images'] as $image) {
if ($image['id'] != '') {
if($counter == 0){
$html .= " <img src='".$image['path']."' alt=''/>\n";
$counter++;
}else{
$html .= " <a href='".$image['path']."' class='image-wrap' title='' rel='prettyPhoto'>".$image['id']."\n";
$html .= " <span class='zoom-icon'></span>\n";
$html .= " </a>\n";
}
} else {
$html .= " <a href='".$image['path']."' title='1' rel='prettyPhoto'group''></a>\n";
}
}
echo $html;
?>
case 2: when index starts from 0.
foreach($property['images'] as $key=>$image) {
if ($image['id'] != '') {
if($key == 0){
$html .= " <img src='".$image['path']."' alt=''/>\n";
}else{
$html .= " <a href='".$image['path']."' class='image-wrap' title='' rel='prettyPhoto'>".$image['id']."\n";
$html .= " <span class='zoom-icon'></span>\n";
$html .= " </a>\n";
}
} else {
$html .= " <a href='".$image['path']."' title='1' rel='prettyPhoto'group''></a>\n";
}
}
I want to merge array which have same key to be one. Example
$options = array(
array("group" => "header","title" => "Content 1"),
array("group" => "header","title" => "Content 2"),
array("group" => "menu","title" => "Content 3"),
array("group" => "content","title" => "Content 4"),
array("group" => "content","title" => "Content 5"),
array("group" => "content","title" => "Content 6"),
array("group" => "footer","title" => "Content 7")
);
foreach ($options as $value) {
if ($value['group']) {
echo "<div class='{$value['group']}'>";
echo $value['title'];
echo "</div>";
}
}
Current output is
<div class='header'>Content 1</div><div class='header'>Content 2</div><div class='menu'>Content 3</div><div class='content'>Content 4</div><div class='content'>Content 5</div><div class='content'>Content 6</div><div class='footer'>Content 7</div>
What I want here is to be
<div class='header'>
Content 1
Content 2
</div>
<div class='menu'>
Content 3
</div>
<div class='content'>
Content 4
Content 5
Content 6
</div>
<div class='footer'>
Content 7
</div>
Let me know
$grouped = array();
foreach($options as $option) {
list($group, $title) = array_values($option);
if (!isset($grouped[$group])) {
$grouped[$group] = array();
}
$grouped[$group][] = $title;
}
foreach ($grouped as $group => $titles) {
echo sprintf('<div class="%s">%s</div>', $group, implode('', $titles));
}
$groups = array ();
foreach ( $options as $value ) {
if ( !isset ( $groups[$value['group']] ) ) {
$groups[]['group'] = $value['group']
}
$groups[$value['group']]['title'][] = $value['title'];
}
foreach ( $groups as $group ) {
echo "<div class="{$group['group']}">";
echo implode ( "\n", $group['title'] );
echo "</div>";
}
This should work, but if it doesn't matter to you, you could also just change the structure of your hardcoded-array, then you wouldn't need my first foreach.