I want generate a link using two array: the first one contains addresses; the second one contains text.
I want have:
- text3
- text3
- text3
to do so I tried like this but I can't generate texts.
<ul>
<?php
isset($_GET["page"]) ? $page=$_GET["page"] : $page="home";
$vocimenu=array("address1","address2","address3");
$nomimenu=array("text1","text2","text3");
$nome=array_values($nomimenu);
foreach($vocimenu as $voce) {
echo "<li>";
if($page!=$voce) echo '<a href="?page='.$voce.'">';
echo $nome;
if($page!=$voce) echo "</a>";
echo "</li>";
}
?>
</ul>
You can use one array
isset($_GET["page"]) ? $page=$_GET["page"] : $page="home";
$links=array("address1"=>"text1","address2"=>"text2","address3"=>"text3");
foreach($links as $href=>$text){
if($page!=$voce){
echo ''.$text.'';
}else{
echo $text;
}
}
This should work:
isset($_GET["page"]) ? $page=$_GET["page"] : $page="home";
$vocimenu=array("address1","address2","address3");
$nomimenu=array("text1","text2","text3");
//since you're using two arrays, foreach is not the way to go
//you need a counter so you can get elements from each array
for ($i=0;$i<count($vocimenu);$i++) {
echo "<li>";
if($page!=$voce) echo '<a href="?page='.$vocimenu[$i].'">';
echo $nomimenu[$i];
if($page!=$voce) echo "</a>";
echo "</li>";
}
An alternate option is to do it like this, but that could make some of your other code less flexible:
$array = array("address1"=>"value1","address2"=>"value2",...);
foreach($array as $address=>$value){
echo "<li>";
if($page!=$voce) echo '<a href="?page='.$address.'">';
echo $value;
if($page!=$voce) echo "</a>";
echo "</li>";
}
It would be much easier if you create an associative array:
$menu = array(
"fmp_trama" => "Full Metal Panic!",
"fumoffu_trama" => "Full Metal Panic? Fumoffu",
"fmp_tsr" => "Full Metal Panic! TSR"
);
echo '<ul>';
foreach ($menu as $key => $value) {
echo "<li>";
if($page != $key) {
echo sprintf('%s', $key, $value);
}
else {
echo sprintf('<span>%s</span>', $value);
}
echo "</li>";
}
echo '</ul>';
You can build the array like this: (if you are bound to the 2 array structure)
$menu = array_combine($vocimenu, $nomimenu);
Related
This is working but dont seems the right way to code
I have no idea about what to do
foreach ($data as $tag) {
if ($tag==1) echo "<a onclick=\"window.location.href='/buscatag/1'\">Coméda</a>,\n";
if ($tag==2) echo "<a onclick=\"window.location.href='/buscatag/2'\">Ecchi</a>,\n";
if ($tag==3) echo "<a onclick=\"window.location.href='/buscatag/3'\">Peitos grandes</a>,\n";
if ($tag==4) echo "<a onclick=\"window.location.href='/buscatag/4'\">Nudez</a>,\n";
if ($tag==5) echo "<a onclick=\"window.location.href='/buscatag/5'\">Seinen</a>,\n";
if ($tag==6) echo "<a onclick=\"window.location.href='/buscatag/6'\">Violência<a/>,\n";
if ($tag==7) echo "<a onclick=\"window.location.href='/buscatag/7'\">Vida Cotidiana</a>,\n";
if ($tag==8) echo "<a onclick=\"window.location.href='/buscatag/8'\">Harém</>,\n";
if ($tag==9) echo "<a onclick=\"window.location.href='/buscatag/9'\">Ação</a>,\n";
if ($tag==10) echo "<a onclick=\"window.location.href='/buscatag/10'\">Shonen</a>,\n";
if ($tag==11) echo "<a onclick=\"window.location.href='/buscatag/11'\">Super Poderes</a>,\n";
if ($tag==12) echo "<a onclick=\"window.location.href='/buscatag/12'\">Aventura</a>,\n";
if ($tag==13) echo "<a onclick=\"window.location.href='/buscatag/13'\">Fantasia</a>,\n";
if ($tag==14) echo "<a onclick=\"window.location.href='/buscatag/14'\">Loli</a>,\n";
}
// Define $lookup as
$lookup = [1 => 'Coméda', 2 => 'Ecchi',]; // etc
// Note the relation between key and value
foreach ($data as $tag) {
echo "<a onclick=\"window.location.href='/buscatag/{$tag}'\">" . $lookup[$tag] . "</a>,\n";
}
in code you can immediately put the number into a tag
try
$names = ['name1', 'name2'...];
foreach ($data as $tag) {
echo sprintf("<a onclick=\"window.location.href='/buscatag/%s'\">%s</a>,\n", $tag, $names[$tag-1]);
}
When you're going to test the same variable many times you can use a switch to make it better:
foreach ($data as $tag) {
switch ($tag) {
case 1:
echo "<a onclick=\"window.location.href='/buscatag/1'\">Coméda</a>,\n";
break;
case 2:
echo "<a onclick=\"window.location.href='/buscatag/2'\">Ecchi</a>,\n";
break;
}
}
I'm Trying to format the output of an array using php but I can't seem to get the keys and values on the same line. I've listed the code that I'm using to display the keys and values but this code outputs the keys and values on different lines
function olLiTree($tree)
{
echo '<pre>';
foreach($tree as $key => $item) {
if (is_array($item)) {
echo '<pre>', $key ;
olLiTree($item);
echo '</pre>';
} else {
echo '<pre>', $item, '</pre>';
}
}
echo '</ul>';
}
print(olLiTree($results));
Use <ul> <li> .... </li></ul>. Also remove comma(,) because PHP use dot(.) for concat string.
function olLiTree($tree)
{
echo '<ul>';
foreach($tree as $key => $item) {
if (is_array($item)) {
echo '<li>'. $key ;
olLiTree($item);
echo '</li>';
} else {
echo '<li>' .$item. '</li>';
}
}
echo '</ul>';
}
print(olLiTree($results));
You use comma , instead of dot .
for string concatenation php use dot
function olLiTree($tree)
{
echo '<pre>';
foreach($tree as $key => $item) {
if (is_array($item)) {
echo '<pre>'. $key ;
olLiTree($item);
echo '</pre>';
} else {
echo '<pre>' . $item. '</pre>';
}
}
echo '</ul>';
}
print(olLiTree($results));
Needed Navigation Html
Home
Pages
About
Services
Products
Contact
FAQs
Sitemap
Privacy Policy
Column Layouts
1 Column
2 Column (Left Sidebar)
2 Column (Right Sidebar)
3 Column
4 Column
I want to use php arrays and foreach loops to output the needed html.
The php code I have thus far is:
<?php
$data = array("navigation");
$data['navigation']['Home'] = base_url();
$data['navigation']['Pages'] = base_url('pages');
$data['navigation']['Pages']['About'] = base_url('pages/about');
echo '<ul>';
foreach($data as $nav) {
foreach($nav as $subNavKey => $subNavHref) {
echo "<li><a href='$subNavHref'>$subNavKey</a>";
}
}
echo '</ul>';
?>
I was thinking I would need three foreach loops nested but php warnings/errors are generated when the third loop is reached on lines such as:
$data['navigation']['Home'] = base_url();
$data['navigation']['Pages'] = base_url('pages');
I'm not quite sure how to test for 3rd level depths such as:
$data['navigation']['Pages']['About'] = base_url('pages/about');
Also, outputting the needed li and ul tags in the proper positions has given me trouble aswell.
Use recursion
$data['navigation']['Home'] = base_url();
$data['navigation']['Pages'] = base_url('pages');
$data['navigation']['Pages']['About'] = base_url('pages/about');
$data['navigation']['Pages']['About']['Team'] = base_url('pages/team');
$data['navigation']['Pages']['About']['Team']['Nate'] = base_url('pages/nate');
echo "<ul>"
print_list($data);
echo "</ul>"
function print_list($menu) {
foreach($menu as $key=>$item) {
echo "<li>";
if(is_array($item)) {
echo "<ul>";
print_list($item);
echo "</ul>";
} else {
echo "<a href='{$val}'>$key</a>";
}
echo "</li>";
}
}
<?php
function nav($data) {
$html = '<ul>';
foreach ($data as $k => $v) {
if (is_array($v)) {
$html .= "<li>$k" . nav($v) . "</li>";
}
else {
$html .= "<li><a href='$k'>$v</a>";
}
}
$html .= '</ul>';
return $html;
}
echo nav($data);
A recursive function can get the job done:
$items = array(
"Home",
"Pages" => array(
"About",
"Services",
"Products",
"Contact",
"FAQs",
"Sitemap",
"Privacy Policy",
"Column Layouts" => array(
"1 Column",
"2 Column (Left Sidebar)",
"2 Column (Right Sidebar)",
"3 Column",
"4 Column"
)
)
);
function getMenu($array) {
foreach($array as $key => $value) {
if(is_array($value)) {
echo "<li>" . $key . "</li>";
echo "<ul>";
getMenu($value);
echo "</ul>";
} else {
echo "<li>" . $value . "</li>";
}
}
}
echo "<ul>";
getMenu($items);
echo "</ul>";
Output:
You should use a recursive function, for example (Working Demo):
function makeMenu($array)
{
$menu = '';
foreach($array as $key => $value) {
if(is_array($value)) {
$menu .= '<li>' . $key . '<ul>' . makeMenu($value) . '</ul></li>';
}
else {
$menu .= "<li><a href='". $value ."'>" . $value ."</a></li>";
}
}
return $menu;
}
Then call it like:
$data = array(
"Home",
"Pages" => array("About", "Services"),
"Column Layouts" => array("1 Column", "2 Column (Left Sidebar)")
);
echo '<ul>' . makeMenu($data) . '</ul>';
I would like to assign an image to a variable in a PHP script so that I can make the image appear when I want to it to, by declaring the variable.
$FoodList = array_unique($FoodList);
if (!empty($FoodList)) {
foreach ($FoodList as $key => $value) {
// The variable would go here, so that image would appear
//next to each variable
echo "<li>" . $value . "<li>";
}
echo "</ul>";
}
Either you assign
$var = "img src="'your/pathto/image.ext'";
$var = "your/pathto/image.ext";
and echo it in html img code
The second method is more preferred
$FoodList = array_unique($FoodList);
if(!empty($FoodList)) {
foreach ($FoodList as $key => $value) {
//The variable would go here, so that image would appear
//next to each variable
$value = "<li>";
//Maybe you'll only display an image is a certain condition is met? If so, then...
if($condition == "parameter") {
$value .= "<img src='path/to/img' alt='img' />";
}
$value .= "</li>";
echo $value;
unset($value);
}
echo "</ul>";
}
$FoodList=array_unique($FoodList);
$img_path = 'images/example.jpg';
if(!empty($FoodList))
{
foreach ($FoodList as $key => $value)
{
echo "<img src='$img_path' />";
echo "<li>".$value."<li>";
}
echo "</ul>";
}
Use this:
echo "<li><img src='path_of_image/".$value."'/><li>";
Supposing that $value has the name of your image with extension of image.
<?php
$name="Adil";
echo $name;
$path="FB_IMG_1465102989930.jpg";
for($i=0;$i<44;$i++)
{
echo($i.'<br>') ;
if($i==10)
{
echo ".$path.";
echo "<img src ='".$path."'>";
}
}
?>
please insert a space before your image name :-
Example:-
$image_name="myphoto.jpg";
$image_path="./upload/ ".$image_name;
here I add a space after "./upload/(space)"
Store the image path into your MySql database.
call it from your HTML page as:-
<img src= '<?php echo $image_path;?>'width="200" height="200" alt=""/>
I have a multi dimensional array with the structure:
Name
Address
Area
Area Type 1
Area Type 2
Area Type 2
I have a loop that grabs the Name and Address for each business and echos them out. Now what I want to be able to do is now grab the area and chuck that out for each business. My loop thus far is as follows:
foreach ($data AS $key => $value) {
echo '<ul>';
echo '<li>';
echo $value['Name'];
echo '</li>';
echo '<li>';
echo $value['Address'];
echo '</li>';
echo '<li>';
foreach ($data as $row) {
echo $data['Area'];
}
echo '</li>';
echo '</ul>';
}
I can output all the Areas in one go with:
foreach($data as $row)
{
foreach($row['Area'] as $areaout)
{
echo $areaout;
}
}
But I need it to echo out with it's respective name and address
Shouldn't you simply replace
foreach ($foo as $row) {
echo $foo['Area'];
}
by
echo '<ul>';
foreach ($value['Area'] as $v) {
echo '<li>' . $v . '</li>';
}
echo '</ul>'
?