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);
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>";
}
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 } ?>
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
I have some array:
array
0 => string 'Apple'
1 => string 'banana'
2 => string 'Nanas'
3 => string 'Grape'
What is the best way to display
1. Apple
2. banana
3. Nanas
4. Grape
Any Ideas?
If the output you are after is HTML, then an ordered list makes sense:
<ol>
<?php foreach($fruits as $fruit) { ?>
<li><?php echo $fruit; ?></li>
<?php } ?>
</ol>
How about this?
foreach($fruits as $index => $fruit) {
echo ($index+1).". ".$fruit."\n";
}
If HTML
<ol>
<?php
foreach ($fruits as $fruit) {
echo "<li>", $fruit;
}
?>
</ol>
i want to write a generalized function that can output something like this.
<span class="side_title">By Collection</span>
<ul class="menu">
<li class="top">
<a class="top_menu" href="#url">home</a>
</li>
<li class="sub">
collectionA
<ul>
<li>collectionA-1</li>
<li>collectionA-2</li>
</ul>
</li>
<li class="sub">
collectionB
<ul>
<li>collectionB-1</li>
</ul>
</li>
<li class="top">CollectionE</li>
</ul>
<span class="side_title">By Functions</span>
<ul class="menu">
<li class="top">
<a class="top_menu" href="#url">home</a>
</li>
<li class="sub">
functionA
<ul>
<li>functionA-1</li>
<li>functionA-2</li>
</ul>
</li>
<li class="sub">
functionB
<ul>
<li>functionB-1</li>
<li>functionB-2</li>
</ul>
</li>
<li class="top">functionE</li>
</ul>
but the problem is i can't find a data structure to represent this structure, i tried the follwoing(using array - where the key is link's content, value is the address. and recursion):
$a = array (
"By collection" => '',
'Base' => 'thisBase',
"expand" => array (
'Home' => 'home',
'collectionA' => 'collectionA',
'expand' => array (
'collectiona-1' => 'collectiona-1',
'collectiona-2' => 'collectiona-2'
),
'collectionB' => 'collectionb',
'expand' => array (
'collectionb-1' => 'collectionb-1',
'collectionb-2' => 'collectionb-2'
),
'collectionB' => 'collectionb',
'expand' => array (
'collectionc-1' => 'collectionc-1',
'collectionc-2' => 'collectionc-2'
),
),
"by Function" => ''
);
function expand($a=NULL)
{
foreach ($a as $key => $value)
{
if ($value == '')
{
echo '<span class="side_title">'.$key.'</span>';
echo '<ul class="menu">';
} elseif (is_array($value))
{
echo '<ul class="sub_menu">';
expand($value);
} else {
echo '<li><a href="'.$value.'>'.$key.'</a></li>';
}
}
echo '</ul>';
}
print_r($a);
expand($a);
i thought about using xml/tree to represent it, but the data structure is pass between different function, so i thought it will be to much effort, so i want to know what's wrong with this? or is there better way?
I don't know, if this solves your problem, but you forgot a closing " for the href attribute.
echo '<li>'.$key.'</li>';