I want to show one dropdown with the values from product attribute. But always is showing the first position empty. I have 2 values but I don't know why the array have 3 positions
<?php
$options = Mage::getSingleton('eav/config')->getAttribute('catalog_product', 'tipo_paquete')->getSource()->getAllOptions();
var_dump($options);
?>
<select id="tipo_paquete" class="required select" name="tipo_paquete">
<option value=""><?php echo $helper->__('--Please Select--')?></option>
<?php
foreach ($options as $option)
{
echo "<option value='".$option['value']."'>". $option['label'] ."</option>";
}
?>
</select>
This code show the select like this:
And the var_dump show this:
array(3) { [0]=> array(2) { ["label"]=> string(0) "" ["value"]=> string(0) "" } [1]=> array(2) { ["value"]=> string(1) "8" ["label"]=> string(15) "Caja de cartón" } [2]=> array(2) { ["value"]=> string(1) "7" ["label"]=> string(14) "Caja de madera" } }
I don't know why I have 3 positions, I only saved 2 options. I tested with other attributes with the same problem.
I've found the solution here. getAllOptions can recieve two parameters:
array getAllOptions ([bool $withEmpty = true], [bool $defaultValues = false])
The $withEmpty adds an empty option to array
Just pass false to getAllOptions().
$options = Mage::getSingleton('eav/config')->getAttribute('catalog_product', 'tipo_paquete')->getSource()->getAllOptions(false);
Related
I am performing a select query from the database and store the result into an array like so:
$res[] = $row;. Then in my view i get:
Notice: Array to string conversion in C:\xampp\htdocs ...
In my view
<?php foreach($res as $PARENT_CAT_CODE => $PARENT_CAT_DESC): ?>
<option value="<?= $PARENT_CAT_CODE; ?>"><?= $PARENT_CAT_DESC; ?></option>
<?php endforeach; ?>
var_dump($res); (Output)
array(4) {
[0]=>
array(2) {
["PARENT_CAT_CODE"]=> string(3) "100"
["PARENT_CAT_DESC"]=> string(16) "ROUTE COLLECTION"
}
[1]=>
array(2) {
["PARENT_CAT_CODE"]=> string(1) "1"
["PARENT_CAT_DESC"]=> string(5) "SHOES"
}
[2]=>
array(2) {
["PARENT_CAT_CODE"]=> string(1) "4"
["PARENT_CAT_DESC"]=> string(41) "ΑΝΤΑΛΛΑΚΤΙΚΑ ΟΧΗΜΑΤΩΝ"
}
[3]=>
array(2) {
["PARENT_CAT_CODE"]=> string(1) "6"
["PARENT_CAT_DESC"]=> string(10) "ΒΑΦΕΣ"
}
}
the query:
<?php
$categories_PST = oci_parse($conn, 'SELECT DISTINCT PARENT_CAT_CODE,PARENT_CAT_DESC FROM V_SCT_ITEM ORDER BY PARENT_CAT_DESC');
oci_execute($categories_PST);
while ( $row = oci_fetch_array($categories_PST) )
{
$res[] = $row;
}
You looped it wrong,
$val is an array which you will get in loop with data you are asking for to populate in option tag. This should work.
When you loop 2D array in a single loop, its instance/loop val I can say, will have immediate next level of array and you can use it as below you want.
<?php foreach ($res as $val): ?>
<option value="<?= $val['PARENT_CAT_CODE']; ?>" > <?= $val['PARENT_CAT_DESC']; ?></option>
<?php endforeach;?>
It's hard to explain it with words, but what I want to do is skip the ranking when there is no ranking information in CakePhp2. For example, I have the following data that contains 7 rows of ranking data. 2 out of the 7 row contains the empty body(2nd and the 5th row). So I made a script to skip the ranking post that is empty. However, Since the 2nd and the 5th rows that have been skipped. The data post that is displayed is 1,3,4,6,7. But I want to the display the ranking like 1,2,3,4,5. In other words, I want to show the 3rd ranking as 2nd and 4th as 3rd and so forth. Sorry for my poor explanation. Simply, I want to skip ranking when there is no ranking information and show the ranking number. (PS: I don't want to alter the DB) I will love to hear from you!
array(7) { [0]=> array(1) { ["Post"]=> array(5) { ["id"]=> string(1) "1" ["title"]=> string(6) "title1" ["body"]=> string(5) "body1" ["created"]=> string(19) "2017-04-04 21:25:43" ["modified"]=> NULL } } [1]=> array(1) { ["Post"]=> array(5) { ["id"]=> string(1) "2" ["title"]=> string(0) "" ["body"]=> string(0) "" ["created"]=> string(19) "2017-04-04 21:25:43" ["modified"]=> NULL } } [2]=> array(1) { ["Post"]=> array(5) { ["id"]=> string(1) "3" ["title"]=> string(6) "title3" ["body"]=> string(5) "body3" ["created"]=> string(19) "2017-04-04 21:25:43" ["modified"]=> NULL } } [3]=> array(1) { ["Post"]=> array(5) { ["id"]=> string(1) "4" ["title"]=> string(6) "title4" ["body"]=> string(5) "body4" ["created"]=> string(19) "2017-04-08 15:48:21" ["modified"]=> NULL } } [4]=> array(1) { ["Post"]=> array(5) { ["id"]=> string(1) "5" ["title"]=> string(0) "" ["body"]=> string(0) "" ["created"]=> string(19) "2017-04-08 16:14:08" ["modified"]=> NULL } } [5]=> array(1) { ["Post"]=> array(5) { ["id"]=> string(1) "6" ["title"]=> string(6) "title6" ["body"]=> string(5) "body6" ["created"]=> string(19) "2017-04-08 16:14:08" ["modified"]=> NULL } } [6]=> array(1) { ["Post"]=> array(5) { ["id"]=> string(1) "7" ["title"]=> string(6) "title7" ["body"]=> string(5) "body7" ["created"]=> string(19) "2017-04-08 16:14:08" ["modified"]=> NULL } } }
Trying to make a script to show the Popular ranking. I really love to hear about some great Hints and samples from you!
<h1>Popular Ranking posts</h1>
<?php $k = 1; ?>
<?php for($i = 0; $i <= count($posts); $i++) { ?>
<ul>
<?php if (!empty($posts[$i]['Post']['body'])) { ?>
<h2><?php echo $posts[$i]['Post']['title']; ?></h2>
<li>
<?php
echo $posts[$i]['Post']['body'];
?>
</li>
//Want to show the number 2 in $k even though the 2nd body data is missing(Currently 3rd data).
<h3 class="ranking_number"><?php echo $k; ?></h3>
<?php } else {
continue;
}?>
</ul>
<?php $k++; } ?>
<h1>Popular Ranking posts</h1>
<?php $k = 1; ?>
<?php for($i = 0; $i <= count($posts); $i++) { ?>
<ul>
<?php if (!empty($posts[$i]['Post']['body'])) { ?>
<h2><?php echo $posts[$i]['Post']['title']; ?></h2>
<li>
<?php
echo $posts[$i]['Post']['body'];
?>
</li>
//Want to show the number 2 in $k even though the 2nd body data is missing(Currently 3rd data).
<h3 class="ranking_number"><?php echo $k; ?></h3>
<?php $k++; } ?>
<?php } else {
continue;
}?>
</ul>
What was changed?
I moved $k++; inside of if, just before else
Why?
What was your code doing:
Set $k to 1.
Post 1 isn't empty, so show it (rank 1).
Increase $k by 1.
Go to next post
Post 2 is empty, so don't show it (rank 2).
Increase $k by 1.
Let's stop there. Your $k was increased with every post, even those skipped, so the third position was 3rd instead of 2nd.
What is my code doing:
Set $k to 1.
Post 1 isn't empty, so show it (rank 1).
Increase $k by 1.
Go to next post
Post 2 is empty, so don't show it (rank 2).
Go to next post
Post 3 isn't empty, so show it (still rank 2).
I hope I helped.
I have a web form with checkboxes for Event Spaces.
The checkbox values are grabbed from one table in my database "facility_event_charges":
Let's say I check 5 and 6, "Audio Visual Equipment" and "Audio Visual Technician" and submit my form.
This will save the id(s) to a separate table that I have for that "Event Space"
Let's say my "Event Space" has an ID of 63. The lookup table now has two entries for my id since I checked 2 of the checkboxes:
All is good and saved.
Now, let's say I need to actually edit the charges associated with this space.
When I click edit, my webform renders the form fields BUT the checkboxes are empty!! Why? Because it is grabbing the data from my "facility_event_charges" table.
I need to compare this table data (array) with the data saved in my lookup table (array) to figure out which values are in common and render a check in those checkboxes.
Desired result in my edit space webform would have those checked
My checkbox "charges" array:
array(6) {
[1]=> string(9) "Officials"
[2]=> string(6) "Ushers"
[3]=> string(19) "Additional Staffing"
[4]=> string(16) "Special Lighting"
[5]=> string(22) "Audio Visual Equipment"
[6]=> string(23) "Audio Visual Technician"
}
--
I can also generate the charges in this:
array(6) {
[0]=> array(1) { ["FacilityEventCharge"]=> array(2) { ["id"]=> string(1) "1" ["type"]=> string(9) "Officials" } }
[1]=> array(1) { ["FacilityEventCharge"]=> array(2) { ["id"]=> string(1) "2" ["type"]=> string(6) "Ushers" } }
[2]=> array(1) { ["FacilityEventCharge"]=> array(2) { ["id"]=> string(1) "3" ["type"]=> string(19) "Additional Staffing" } }
[3]=> array(1) { ["FacilityEventCharge"]=> array(2) { ["id"]=> string(1) "4" ["type"]=> string(16) "Special Lighting" } }
[4]=> array(1) { ["FacilityEventCharge"]=> array(2) { ["id"]=> string(1) "5" ["type"]=> string(22) "Audio Visual Equipment" } }
[5]=> array(1) { ["FacilityEventCharge"]=> array(2) { ["id"]=> string(1) "6" ["type"]=> string(23) "Audio Visual Technician" } } }
My lookup table array:
array(2) {
[0]=> array(1) { ["EventCharge"]=> array(1) { ["facility_event_charges_id"]=> string(1) "5" } }
[1]=> array(1) { ["EventCharge"]=> array(1) { ["facility_event_charges_id"]=> string(1) "6" } }
}
--
I've been unsuccessful in my attempts - tried php array_intersect
array_intersect_assoc
array_map('array_diff_assoc')
I need to loop through the checkbox charges array and find a match from the lookup table array, when it matches, mark the checkbox "checked".
Anyone have any working examples I could test out?
The checkbox charges array key is what would need to match the "facility_event_charges_id"
As per you array given above you can compare like this
for example Lets take $charge array as
array(6) {
[1]=> string(9) "Officials"
[2]=> string(6) "Ushers"
[3]=> string(19) "Additional Staffing"
[4]=> string(16) "Special Lighting"
[5]=> string(22) "Audio Visual Equipment"
[6]=> string(23) "Audio Visual Technician"
}
take $lookup array as
array(2) {
[0]=> array(1) { ["EventCharge"]=> array(1) { ["facility_event_charges_id"]=> string(1) "5" } }
[1]=> array(1) { ["EventCharge"]=> array(1) { ["facility_event_charges_id"]=> string(1) "6" } }
}
You can compare both array like below
$charges = array(
1 => "Officials",
2 => "Ushers",
3 => "Additional Staffing",
4 => "Special Lighting",
5 => "Audio Visual Equipment",
6 => "Audio Visual Technician"
);
$lookup = array(
array("EventCharge" => array(
"facility_event_charges_id" => "5"
)
), array(
"EventCharge" => array(
"facility_event_charges_id" => "6"
)
)
);
$lookup = array_column(array_column($lookup, 'EventCharge'), 'facility_event_charges_id');
foreach ($charges as $key => $value) {
echo '<input type="checkbox" name="charges" value="' . $key . '" '.(in_array($key, $lookup)?"checked":"").'>';
echo '<label>'.$value.'<label>';
}
Edit
If array_column() is not supported than you can create array using loop like below and test
$arr=array();
foreach ($lookup as $key => $value) {
foreach ($value as $k => $v) {
$arr[]=$v['facility_event_charges_id'];
}
}
foreach ($charges as $key => $value) {
echo '<input type="checkbox" name="charges" value="' . $key . '" '.(in_array($key, $arr)?"checked":"").'>';
echo '<label>'.$value.'<label>';
}
This works...
But it will change based on how exactly you end up building the arrays... but that should only require a change in the if-then statement and possibly a change in the value of i (the index variable).
<html>
<head></head>
<body>
<form>
<?php
// assume you build this based on a sql query
// you don't need to use the primary key for the
// array index
$charges[0]="Officials";
$charges[1]="Ushers";
$charges[2]="Additional Staffing";
$charges[3]="Special Lighting";
$charges[4]="AV Equipment";
$charges[5]="AV Technician";
// your array of eventcharges
// select facility_event_charges_id from tbl where facility_event_spaces_id='63'
// loop thru the result of teh query make a simple array of your evencharge ids
// gonna hardcode mine out of laziness
// since php is typeless we can use strings to compare to ints
// directly as long as we use == and not ===
// and in_array() (used below) goes with loose comparison too
$ec[]="4";
$ec[]="5";
// now we have an array of our descriptions
// and an array of the already selected choices
// from the db data
for($i=0;$i<count($charges);$i++){
print('<input type="checkbox" name="charges" value="'.$i.'" ');
if(in_array($i,$ec)){
print("checked");
}
print(" > ".$charges[$i]."<br />");
}
?>
</form>
</body></html>
Hi you can use following code.i assumed that you have got $arr=array('5','6'); from database.so you can us as follows
<?php
$arr=array('5'=>'Audio Visual Equipment','6'=>'Audio Visual Technician');
$arr=array_keys($arr);//if you have associated array
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="ISO-8859-1">
</head>
<body>
<input type="checkbox" name="charges" value="1" <?=in_array('1', $arr)?'checked=true':''?>> Officials<br>
<input type="checkbox" name="charges" value="2" <?=in_array('2', $arr)?'checked=true':''?>> Ushers<br>
<input type="checkbox" name="charges" value="3" <?=in_array('3', $arr)?'checked=true':''?>> Additional Staffing <br>
<input type="checkbox" name="charges" value="4" <?=in_array('4', $arr)?'checked=true':''?>> Special Lighting <br>
<input type="checkbox" name="charges" value="5" <?=in_array('5', $arr)?'checked=true':''?>> Audio Visual Equipment<br>
<input type="checkbox" name="charges" value="6" <?=in_array('6', $arr)?'checked=true':''?>> Audio Visual Technician<br>
</body>
</html>
I am trying to output the results of some blog posts using a foreach loop but I keep getting the error "Undefined index: title"
<?php foreach ($post as $post_item):?>
<div class="postDiv">
<h4><?php echo $post_item['title'];?></h4>
<p><?php echo $post_item['summary'];?></p>
</div>
<?php endforeach;?>
here is my array when calling var_dump($post)
array(4) {
["id"]=> array(5) {
[0]=> string(2) "15"
[1]=> string(2) "16"
[2]=> string(2) "17"
[3]=> string(2) "18"
[4]=> string(2) "19" }
["title"]=> array(5) {
[0]=> string(3) "234"
[1]=> string(2) "11"
[2]=> string(1) "2"
[3]=> string(3) "444"
[4]=> string(5) "title" }
["summary"]=> array(5) {
[0]=> string(3) "213"
[1]=> string(2) "11"
[2]=> string(1) "2"
[3]=> string(3) "444"
[4]=> string(7) "summary" }
["content"]=> array(5) {
[0]=> string(3) "234"
[1]=> string(1) "1"
[2]=> string(1) "2"
[3]=> string(3) "444"
[4]=> string(7) "content" } }
I am a noob at php and I am also using codeigniter if that makes a difference here (I dont think it should). If I use the following code I can print out the posts for the title only but this is not what I want as I want to add in more fields later
<?php foreach ($post['title] as $post_item):?>
<div class="postDiv">
<h4><?php echo $post_item;?></h4>
</div>
<?php endforeach;?>
Here is my final code that worked for me
<?php
foreach (array_reverse(array_keys($post["id"]), true) as $key):?>
<div class="postDiv">
<h4><?php echo $post['title'][$key];?></h4>
<p><?php echo $post['summary'][$key];?></p>
<p><?php echo $post['content'][$key];?></p>
<div style="float:right">
<p><?php echo anchor('admin/edit/'.$post['id'][$key],' [edit]');?></p>
<p><?php echo anchor('admin/delete/'.$post['id'][$key],' [delete]');?></p>
</div>
</div>
<?php endforeach;?>
Your array is somehow misaligned for your design.
<?php foreach (array_keys($post["id"]) as $key):?>
<div class="postDiv">
<h4><?php echo $post['title'][$key];?></h4>
<p><?php echo $post['summary'][$key];?></p>
</div>
<?php endforeach;?>
It looks like your post information is split across multiple array keys based on a numeric reference, so you can use that numeric key reference to access the sibling array keys which contain the rest of your data.
foreach($post['id'] as $key => $id) {
$title = $post['title'][$key];
$summary = $post['summary'][$key];
$content = $post['content'][$key];
}
Just for you understanding:
with foreach ($a as $k) you are iterating an array $a, and putting in each iteration, one first level element in $k.
So, in the first iteration,
$post_item will be
array(5) {
[0]=> string(2) "15"
[1]=> string(2) "16"
[2]=> string(2) "17"
[3]=> string(2) "18"
[4]=> string(2) "19" }
in the second, $post_item will be:
array(5) {
[0]=> string(3) "234"
[1]=> string(2) "11"
[2]=> string(1) "2"
[3]=> string(3) "444"
[4]=> string(5) "title" }
and so on.
I dont even think that this secodn level indexes, 0,1,2,3,4,5 has an specific meannig to you, and for sure, if you do that foreach( array_keys($post["id"]) ...) would be the same as just iterating from harcoded 0 to count($post), and if you iterate foreach( $post["id"] as $k) and then use $k as index to the other elements, that wouldnt work alos, since the values of id, are not the keys of any other elements. so you will get many undefined index warnings.
and also im not sure if the values, the right side of every element, are actually any kind of usefull information for you. So, we could find some tricks here to get the result that you want, but what you need to fix is one step before, when you create $post
I am noticing some strange behavior while looping through some data. I'm sure it's something simple, but I can't seem to be able to find the bug.
I have the following logic:
<?php
print 'dumping data : <BR>';
var_dump($portvlan);
print '<BR>';
print 'looping through data: <BR>';
foreach ($portvlan as $vlandetail){
echo 'Vlanid: '.$vlandetail['VlanId'].'<BR>';
echo 'Name: '.$vlandetail['Name'].'<BR>';
echo 'Mode: '.$vlandetail['Mode'].'<BR>';
}
?>
This is the output that I'm getting:
dumping data :
array(3) { ["VlanId"]=> string(2) "33" ["Name"]=> string(6) "USR_33" ["Mode"]=> string(6) "Access" }
looping through data:
Vlanid: 3
Name: 3
Mode: 3
Vlanid: U
Name: U
Mode: U
Vlanid: A
Name: A
Mode: A
What I was expecting was to see it print a row with 3 cells, with the following values:
33, USR_33, Access.
Can you tell me where I'm going wrong?
Thanks.
EDIT 1
This logic works fine when the $portvlan array has more than one entry.
For example, on another set of data, the var_dump gives this result:
array(6) { [0]=> array(3) { ["VlanId"]=> string(1) "1" ["Name"]=> string(1) "1" ["Mode"]=> string(5) "Trunk" } [1]=> array(3) { ["VlanId"]=> int(2) ["Name"]=> int(2) ["Mode"]=> string(5) "Trunk" } [2]=> array(3) { ["VlanId"]=> int(3) ["Name"]=> int(3) ["Mode"]=> string(5) "Trunk" } [3]=> array(3) { ["VlanId"]=> int(4) ["Name"]=> int(4) ["Mode"]=> string(5) "Trunk" } [4]=> array(3) { ["VlanId"]=> int(5) ["Name"]=> int(5) ["Mode"]=> string(5) "Trunk" } [5]=> array(3) { ["VlanId"]=> string(2) "33" ["Name"]=> string(2) "33" ["Mode"]=> string(5) "Trunk" } }
And the loop logic works fine.
$vlandetail will be populated with a different item from the array on every iteration of the loop. You shouldn't be treating $vlandetail as an array. Just use it directly.
To get the key name of the array entry, you have to change the loop structure to this:
foreach ($portvlan as $key => $vlandetail) {
echo $key . ': ' . $vlandetail . '<br>';
}
How you get your $portvlan?
If you can know is $portvlan is not an array of $vlandetail, you can simply do this,
$portvlan = array($portvlan);
// start loop
Or you can do this before you loop it, to make sure $portvlan is a numerical array formed by $vlandetail
$portvlan = (isset($portvlan[0]))?$portvlan:array($portvlan);
// start loop