I'm looking to display a list of available weapons of a game, based on items listed in an array. I'm able to display information suck as 'Attribute Values' and 'Price' perfectly via a foreach loop, however being a PHP newbie, I'm having a lot of trouble working out how to echo each item's ID, such as 'Short Sword', 'Middle Sword', 'Long Sword', etc. I thought I was getting close using key($sword)... but no dice. Here's what I'm working with:
<?php $item_swords = Array();
$item_swords["Short Sword"] = Array (
"Attribute Value" => 5,
"Price" => 100,
);
$item_swords["Middle Sword"] = Array (
"Attribute Value" => 8,
"Price" => 250,
);
$item_swords["Long Sword"] = Array (
"Attribute Value" => 12,
"Price" => 750,
); ?>
<?php foreach ($item_swords as $sword) { ?>
<li>
<img src="<?php echo $sword["Item Sprite"]; ?>">
<span><?php echo $sword; ?></span>
<div>ATT +<?php echo $sword["Attribute Value"]; ?></div>
<div><?php echo $sword["Price"]; ?>G</div>
</li>
<?php } ?>
If anyone could lend a hand and help demonstrate how best to echo an item's ID or a per item basis throughout my loop, then that would be very awesome indeed. Thank you for your time.
try with this code
use key attr in foreach loop
and echo $key value instead of array like $sword
<?php $item_swords = Array();
$item_swords["Short Sword"] = Array (
"Attribute Value" => 5,
"Price" => 100,
);
$item_swords["Middle Sword"] = Array (
"Attribute Value" => 8,
"Price" => 250,
);
$item_swords["Long Sword"] = Array (
"Attribute Value" => 12,
"Price" => 750,
); ?>
<?php foreach ($item_swords as $key=>$sword) { ?>
<li>
<img src="
<?php //echo $sword["Item Sprite"]; ?>">
<span><?php echo $key; ?></span>
<div>
ATT +
<?php echo $sword["Attribute Value"]; ?></div>
<div>
<?php echo $sword["Price"]; ?>G
</div>
</li>
<?php } ?>
Related
Question in short: i want to output array: 0, 1 , 2 then break, echo something , output 3,4,5 and keep this in a loop.
Hello everyone i am working on a dynamic php website with bootstrap 4 and php to practice the language. Unfortunately i am now stuck since i dont know how to create something that kindoff looks like an algorith. Allright enough talking and lets get down to the code:
nav.php file
<ul class="navbar-nav">
<li class='nav-item dropdown'>
<a class='nav-link dropdown-toggle' data-toggle='dropdown' datatarget='dropdown_target' href='#'>
<span class='caret'></span>Dropdown
</a>
<div class="dropdown-divider"></div>
<div class="dropdown-menu" aria-labelledby="dropdown_target">
<!-- <a class="dropdown-item">Dropdown</a> -->
<?php
$i=0;
foreach ($dropItems as $item ) {
echo "<a class='dropdown-item' href=\"$item[slug]\">$item[title] </a>";
$i++;
if($i==1) break;
echo "<a class='dropdown-item'>Dropdown</a>";
}
?>
</div>
</li>
</li>
<?php
foreach ($navItems as $item ) {
echo "<li class='nav-item'> <a class='nav-link' href=\"$item[slug]\">$item[title]</a> </li>";
}
?>
</ul>
arrays.php
<?php
//Navigatie menu items
$navItems = array(
array(
"slug" => "index.php",
"title" => "home"
),
array(
"slug" => "contact.php",
"title" => "Contact"
),
array(
"slug" => "market.php",
"title" => "Marketplace"
),
);
$dropItems = array(
array(
"slug" => "#",
"title" => "Lps"
),
array(
"slug" => "#",
"title" => "Sps"
),
array(
"slug" => "market.php",
"title" => "Marketplace"
),
);
?>
You can use array_chunk to split the array in the chunks of three and foreach nested with echo "something";.
Can't see in your code where this echo of three is so I just made an example of how to do it.
$arr = range(1,12); //example array
$chunks = array_chunk($arr, 3);
Foreach($chunks as $chunk){
Foreach($chunk as $val){
Echo $val ." ";
}
Echo "\nsomething\n";
}
https://3v4l.org/0VlN0
Thanks to Andreas i got it working using his method
$chunks = array_chunk($dropItems, 2);
Foreach($chunks as $chunk){
Foreach($chunk as $item){
echo "<a class='dropdown-item' href=\"$item[slug]\">$item[title] </a>";
}
Echo "<div class='dropdown-divider'></div>";
}
Here i want display only Maths marks,i don't know how to do, i am trying but i am not able to display:-
<?php
$marks1=array(
array("Maths"=>80,"Physics"=>89,"Chemistry"=>79),
array("Maths"=>90,"Physics"=>78,"Chemistry"=>87),
array("Maths"=>78,"Physics"=>90,"Chemistry"=>79)
);
echo "<ul>";
for($r=0;$r<count($marks1);$r++)
{
echo "<li>";
foreach($marks1[$r] as $key=>$value)
{
echo $key." = ".$value." ";
}
echo "</li>";
echo "<br><br>";
}
echo "</ul>";
?>
Please prefer using foreach() only. And avoid things like echo "<ul>" etc.... Follow the below coding format...
<?php
$marks1 = array(
array("Maths" => 80, "Physics" => 89, "Chemistry" => 79),
array("Maths" => 90, "Physics" => 78, "Chemistry" => 87),
array("Maths" => 78, "Physics" => 90, "Chemistry" => 79)
);
?>
<ul>
<?php foreach($marks1 as $marks) { ?>
<li>
Maths = <?php echo $marks['Maths']; ?>
</li>
<?php } ?>
</ul>
I hope it's the shortest way to do so-
<?php
$marks1=array(
array("Maths"=>80,"Physics"=>89,"Chemistry"=>79),
array("Maths"=>90,"Physics"=>78,"Chemistry"=>87),
array("Maths"=>78,"Physics"=>90,"Chemistry"=>79)
);
foreach($marks1 as $mark){
echo "MATH = ".$mark['Maths']."<br>";
}
?>
Output :
MATH = 80
MATH = 90
MATH = 78
I have the following array in php:
$stats = array(
"Item 1" => 20,
"Item 2" => 30,
"Item 3" => 66,
"Item 4" => 1
);
I need to echo these values, so I try this:
<?
foreach ($stats as $stat => $data) {
echo '
<div class="col-sm-6">
<div class="widget">
<div class="widget-body p-t-lg">
<div class="clearfix m-b-md">
<h1 class="pull-left text-primary m-0 fw-500"><span class="counter" data-plugin="counterUp">'.$data.'</span></h1>
<div class="pull-right watermark"><i class="fa fa-2x fa-tv"></i></div>
</div>
<p class="m-b-0 text-muted">'.$stats[$stat].'</p>
</div>
</div>
</div>
';
}
?>
But I have only the numerical values echoed.
Do you have the solution ?
Thanks.
Since you are using the foreach construct, $stat holds the keys and $data holds the values. So when saying echo $stats[$stat] is equivalent to echoing the value that has the key $stat. If you want to echo the keys you should do this : echo $stat.
Here you are trying to print the index of the array,
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key = array_search('green', $array); // $key = 2;
echo $key;
Use this code to print the key of the array
Pay attention to the options array this array holds an array of properties on the database, there is a call for the database in the script it's not nessecary to add here, it retrieves the data because it's already on there...
$meta_boxes[] = array(
'id' => 'page_metabox',
'fields' => array(
array(
'name' => ''.$slider['properties']['title'].'',
'desc' => 'Upload an image/pattern for the static area.',
'id' => $prefix . 'text_BOMB',
'type' => 'sortable',
'options' => $slider['properties'],
'multiple' => true,
),
);
The data HTML is outputted using a foreach and an input...
<?php foreach ( $field['options'] as $value => $name ) : ?>
<input type="text" name="<?php echo $field['id'].'[]' ?>" id="<?php echo $field['id'] ?>" value="<?php echo $value; ?>" />
<?php endforeach; ?>
This is exactally what I want, I have a list of inputs for each property... then it hit me, what if its a new user and the data isnt on their database yet the foreach wont output anything and then there is no way to write it to the database... this is pretty noobish but that means an easier answer for you guys :p
You could use empty - function:
<?php
if (empty($field['options'])) {
//do something when options are empty
}
else { //the array $field['options'] has something in it
foreach ( $field['options'] as $value => $name ) : ?>
<input type="text" name="<?php echo $field['id'].'[]' ?>" id="<?php echo $field['id'] ?>" value="<?php echo $value; ?>" />
<?php endforeach;
}
?>
I am using the following tutorial to learn how to shuffle in PHP, but the output is ugly. How do I change the output of the shuffle to be more in line with the poll I want to display in list format?
I am using the following tutorial to learn how to do this:
http://www.w3schools.com/php/func_array_shuffle.asp
<?php
$my_array = array("a" => "Dog", "b" => "Cat", "c" => "Horse");
shuffle($my_array);
print_r($my_array);
?
But the output is ugly: Array ( [0] => Cat [1] => Dog [2] => Horse )
How do I make the shuffle array look nice an pretty like this?
<ul>
<li><input id="pollRadioButton1" name="pollAnswerID" type="radio" value="1" /> Answer1 for Poll1</li>
<li class="pollChart pollChart1"> </li>
<li><input id="pollRadioButton2" name="pollAnswerID" type="radio" value="2" /> Answer2 for Poll1</li>
<li class="pollChart pollChart2"> </li>
</ul>
<?php
$my_array = array("a" => "Dog", "b" => "Cat", "c" => "Horse");
shuffle($my_array);
?>
<ul>
<?php foreach ($my_array as $key => $element): ?>
<li>
Whatever you want to do with
<?php echo htmlspecialchars($element) ?>
(<?php echo htmlspecialchars($key) ?>).
</li>
<li class="pollChart pollChart1"> </li>
<?php endforeach ?>
</ul>
print_r() is just mentioned for debugging only.
To get a 'clean' output you have to specify how things should be ouput:
<?php
$my_array = array("a" => "Dog", "b" => "Cat", "c" => "Horse");
shuffle($my_array);
foreach ($my_array as $key=>$val)
echo sprintf('%s: %s<br>'."\n",$key,$val);
?>
In your case the output might be something like:
echo sprintf('<li><input id="pollRadioButton[]" name="%1$s" '.
'type="radio" value="%2$s" />%2$s</li>'."\n".
'<li class="pollChart pollChart1"> </li>'."\n",$key,$val);