I'm pulling 3 colummns from one table and storing them in an array as such to populate a dropdown menu. The ref_code is used to decide which dropdown it will go to ('Module','Customer','Application') while id is the select value and ref_desc is the display text.
foreach ($test as $t) {
$aa[] = array($t->ref_code => array('id'=>$t->id, 'ref_desc'=>$t->ref_desc));
};
I've been trying to retrieve them using $aa['ref_code'] but have not been getting any success. Help please.
This question is similar, but I am unable to retrieve the values I want.
I found a solution on another forum but I will also give your solutions a try later. Thank you very much!
foreach ($aa as $a => $d) {
foreach ($d as $ref_code => $dd) {
echo "<p>". $ref_code ."</p>";
echo "<p>". $dd['ref_desc'] ."</p>";
echo "<p>". $dd['id'] ."</p>";
};
};
Why don't you write it like this? The ref_code (if it's unique) is the key of the array.
foreach ($test as $t) {
$aa[$t->ref_code] = array('id'=>$t->id, 'ref_desc'=>$t->ref_desc);
};
you need a three dimensional array:
dropdown => ids => displaytext
here we go:
// fill in:
$aa = array();
foreach ($test as $t) {
if ( !isset($aa[$t->ref_code]) )
$aa[$t->ref_code] = array();
$aa[$t->ref_code][$t->id] = $t->ref_desc;
};
// have a drop down:
$myDropDown = 'Customer';
foreach ( $aa[$myDropDown] as $id => $displaytext )
{
echo "<option value=\"" . $id . "\">" . $displaytext . "</option>";
}
.... etc.
Related
I have this code to show how may times a champion was picked in a tournament for example ("Gragas : 2 Times" , "Syndra :4 times" , etc)
I'm using api from leaguepedia to recieve the informations
but im stucked right now, I'm having a problem when "echoing" my table to show the results.
So I want that "qtd" be by the "pick" side (Image1) and if there's an easy way to show what I want.
// $Result is a CURL coming from leaguepedia api
$result = json_decode($file_contents);
// Double foreach to access the values from leaguepedia api
foreach ($result as $d) {
foreach ($d as $data) {
// $data->title->Team1Picks is coming from league pedia api as a string separated by "," Ex:("Gragas,Shen,Maokai,etc")
// So I need to explode to an array to count.
$picks[] = explode(",", $data->title->Team1Picks);
}
}
// $mostpicked is an array for me to count how many times a champion was picked
// $f is an array to see the names of the picked champions
foreach ($picks as $pick) {
foreach ($pick as $total) {
$mostPicked[] = $total;
$f[] = $total;
}
}
// Basically here I'm counting how many times a champion was picked Ex : ("Gragas:2","Syndra:4" ,etc)
asort($mostPicked);
$mostPicked = array_count_values($mostPicked);
$name = array_unique($f);
asort($name);
// Foreach to get all unique names from the picks Ex : ("Gragas","Shen",etc) instead of ("Gragas , "Gragas" , etc)
foreach ($name as $champ) {
echo "<tr>";
echo "<td>" . $champ . "</td>";
echo "</tr>";
}
// This foreach to get the number of times a pick was made Ex : ("Gragas 2 Times")
foreach ($mostPicked as $pick) {
echo "<tr>";
echo "<td>" . $pick . "</td>";
echo "</tr>";
}
Here you go, this should do it.
// Hero name => pick count
$heroPicks = array();
foreach ($result as $d) {
foreach ($d as $data) {
//$data->title->Team1Picks is coming from league pedia api as a string separated by "," Ex:("Gragas,Shen,Maokai,etc")
// So i need to explode to an array to count.
$picks = explode(",", $data->title->Team1Picks);
foreach ($picks as $pick) {
$pick = trim($pick);
if (!array_key_exists($pick, $heroPicks)) {
$heroPicks[$pick] = 0;
}
$heroPicks[$pick] += 1;
}
}
}
uasort($heroPicks, function($a, $b) {
return $a - $b;
});
$heroPicks = array_reverse($heroPicks);
echo "Best picks:".PHP_EOL."<br />";
foreach ($heroPicks as $heroName => $pickCount) {
echo $heroName." - ".$pickCount.PHP_EOL."<br />";
}
It seems because you're putting the $pick in a new <tr>
Try adjusting your loops so that you're building an array of $champ=>$qtd then you an iterate over that and build your table as desired.
I have this json file: and I would like to get all the title so my code is: var_dump($json['results'][0]['title']); but it getting just one title, I know I need to do a foreach but I don't know how :( so if you could help me that will be great ! Thanks
Your data is coming back as an array with item's inside of it. This mean's you'll need to loop through this JSON and print each item;
foreach($json['results'] as $movie) {
echo $movie['title'] . "<br />";
}
You will want to loop through all of the JSON results objects and echo or store the 'title' value;
foreach ($json['results'] as $object) {
//Option 1
echo $object['title'];
//Option 2
array_push($titles, $object['title'];
}
Assuming that the variable $json contains your data, I see two equivalent options in order to iterate trough the array
Using for
for($index = 0; $index < count($json['results']); $index++) {
echo $json['results'][$index]['title'] . "\n";
}
Using foreach
foreach($json['results'] as $movie) {
echo $movie['title'] . "\n";
}
$query2 = "SELECT * FROM `Listing` WHERE listingid = '{$myID}'";
if(!($result2 = # mysql_query($query2,$connection)))
echo "query failed<br>";
$result_array = array();
while($row2 = mysql_fetch_assoc($result2))
{
$result_array[] = $row2;
}
foreach ($result_array as $key => $val) {
echo "$key = $val\n";
}
Listing (Table) has 7 fields.
I get a bunch of "0 = Array"
How do I store mysql query results into php array and display them?
I want to have it as array first so i can sort them.
You are actually storing them. The code you have listed is storing them in array of arrays to see them you can modify the for with:
foreach ($result_array as $key => $val) {
echo "$key = " . print_r($val, true) . "\n";
}
$val is actually an array containing all the fields of a row.
I'm relatively new to PHP and I hope you can help me solve my problem. I am selecting out data from a database into an array for timekeeping. Ultimately, I would like to calculate the total number of hours spent on a project for a given customer.
Here is the code to populate a multi-dimensional array:
...
foreach ($record as $data) {
$mArray = array();
$name = $data['user'];
$customer = $data['customer'];
$project = $data['project'];
$hours = $data['hours'];
$mArray[$name][$customer][$project] += $hours;
}
...
I would now like to iterate over $mArray to generate an xml file like this:
...
foreach ($mArray as $username) {
foreach ($mArray[$username] as $customerName) {
foreach ($mArray[$username][$customerName] as $project ) {
echo '<'.$username.'><'.$customerName.'><'.$project.'><hours>'.
$mArray[$username][$customerName][$project].'</hours></'.$project.'>
</'.$customerName.'></'.$username.'>';
}
}
}
This nested foreach doesn't work. Can someone give me a couple of tips on how to traverse this structure? Thank you for reading!
UPDATE:
Based on the comments I've received so far (and THANK YOU TO ALL), I have:
foreach ($mArray as $userKey => $username) {
foreach ($mArray[$userKey] as $customerKey => $customerName) {
foreach ($mArray[$userKey][$customerKey] as $projectKey => $projectName) {
echo '<name>'.$userKey.'</name>';
echo "\n";
echo '<customerName>'.$customerKey.'</customerName>';
echo "\n";
echo '<projectName>'.$projectKey.'</projectName>';
echo "\n";
echo '<hours>'.$mArray[$userKey][$customerKey][$projectKey].'</hours>';
echo "\n";
}
}
}
This is now only providing a single iteration (one row of data).
Foreach syntax is foreach($array as $value). You're trying to use those values as array keys, but they're not values - they're the child arrays. What you want is either:
foreach($mArray as $username) {
foreach($username as ...)
or
foreach($mArray as $key => $user) {
foreach($mArray[$key] as ...)
I need help with some coding. I need to get a list of All Manufacturers with their corresponding magento ID. Is that possible? Please help. thanks. I tried some mods but only get one or the other. If its possible, pls help w/ this one last thing. I thank you in advance
$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'manufacturer');
foreach ( $attribute->getSource()->getAllOptions(true, true) as $option){
$attributeArray[$option['value']] = $option['label'];
}
foreach($attributeArray as $key=>$val){
echo $val;
}
Not sure exactly what format you require this in but the following example should illustrate how to get to the values you need:
$attribute = Mage::getModel('eav/entity_attribute')
->loadByCode('catalog_product', 'manufacturer');
$valuesCollection = Mage::getResourceModel('eav/entity_attribute_option_collection')
->setAttributeFilter($attribute->getData('attribute_id'))
->setStoreFilter(0, false);
$preparedManufacturers = array();
foreach($valuesCollection as $value) {
$preparedManufacturers[$value->getOptionId()] = $value->getValue();
}
if (count($preparedManufacturers)) {
echo "<h2>Manufacturers</h2><ul>";
foreach($preparedManufacturers as $optionId => $value) {
echo "<li>" . $value . " - (" . $optionId . ")</li>";
}
echo "</ul>";
}