Hi can anyone help me through this,. I'm a beginner learning, please help me nest through foreach loop. Here is the code.
<?php
$resource_url = "/app/resources/";
$names = array('Affiliate program','Careers','Corporate info','Eco Initiative','Government Customers','Social Responsibility');
?>
<ul>
<?php foreach ($names as $arr) {
$links = array('affiliate_program','careers','corporate_info','eco','government','responsibility');
foreach($links as $url){
echo "<li><a href=\"";
echo $resource_url;
echo $url;
echo "\">";
echo $arr;
echo "</a></li>";
}
}?>
</ul>
Try this.
$base_url = "/app/resources/";
$names = array('Affiliate program','Careers','Corporate info','Eco Initiative','Government Customers','Social Responsibility');
$links = array('affiliate_program','careers','corporate_info','eco','government','responsibility');
foreach(array_combine($links, $names) as $key => $url){
echo "<li><a href=\"";
echo $base_url;
echo $key;
echo "\">";
echo $url;
echo "</a></li>";
}
You've inserted the $links inside the foreach loop. Basically every time you loop one array item, ie. Affiliate Program, you loop the entire array of $links. Put $links outside the foreach loop or better yet.
<?php
$resource_url = "/app/resources/";
$names = array(
'affiliate_program' => 'Affiliate program',
'careers' => 'Careers',
'corporate_info' => 'Corporate info',
'eco' => 'Eco Initiative',
'government' => 'Government Customers',
'responsibility' => 'Social Responsibility');
?>
<ul>
<?php foreach($names as $href => $arr) {
echo "<li><a href=\"";
echo $href;
echo "\">";
echo $arr;
echo "</a></li>";
}?>
</ul>
You can do this way -
$resource_url = "/app/resources/";
$names = array('Affiliate program','Careers','Corporate info','Eco Initiative','Government Customers','Social Responsibility');
$links = array('affiliate_program','careers','corporate_info','eco','government','responsibility');
foreach(array_combine($links, $names) as $key => $url){
echo "<li><a href=\"";
echo $resource_url;
echo $key;
echo "\">";
echo $url;
echo "</a></li>";
}
Or generate a single array (key => value) and loop through it.
If you want something like this:
click me to see the image
You can simply:
<?php
$resource_url = "/app/resources/";
$names = array('Affiliate program','Careers','Corporate info','Eco Initiative','Government Customers','Social Responsibility');
$links = array('affiliate_program','careers','corporate_info','eco','government','responsibility');
echo("<ul>");
for($i=0; $i < count($names); $i++){
echo "<li><a href='";
echo $resource_url;
echo $links[$i];
echo "'>";
echo $names[$i];
echo "</a></li>";
}
echo("</ul>");
?>
Related
I have 2 arrays: post_titles and posts. How do I print them one after another with foreach?
When I use 1 array, it works fine:
<?php foreach ($titles as $row) { ?>
<?php echo $row['post_title'] ?> <br>
<?php } ?>
I want data to be printed like this:
Title
Post
<br>
Title
Post
<br>
etc.
If each item in the titles and post correspond to each other (eg titles[1] and posts[1], titles[2] and posts[2]), you could use a for loop.
eg.
for($i = 0; $i < count($titles); $i++) {
echo $titles[$i];
echo $posts[$i];
echo "<br>";
}
or
foreach ($titles as $i => $value ){
echo $value ." <br>" . $posts[$i] . " <br>";
}
If the array have the same index key you can use this
<?php
foreach($titles as $key=> $value) {
echo $value . ' - ' $post[$key] . '<br>';
}
?>
If the 2 arrays are in sync i.e. the same length and arr1(0) equates to arr2(0) then its easy
<?php
foreach ($post_titles as $i => $title) {
echo $title . '<br>' . $posts[$i] . '<br>';
}
?>
I'm trying to iterate through the array using php. I want the items to be stored in an <li> tag. I'm having trouble getting the names to print out. I can get a count of the array items printed, however not the names.
<ul>
<?php
$names = array('Mike', 'Chris', 'Jane', 'Bob');
for($i=0; $i < count($names); $i++) {
echo "<li>" . $i . "</li>";
}
?>
</ul>
<ul>
<?php
$names = array('Mike', 'Chris', 'Jane', 'Bob');
foreach ($names as $name) {
echo "<li>" . $name . "</li>";
}
?>
</ul>
This is a little more natural than both imploding and looping through the array by index.
Your code wasn't working was because you were echoing the index of the name in the array rather than the actual name
<ul>
<?php
$names = array('Mike', 'Chris', 'Jane', 'Bob');
foreach($names as $name) {
echo "<li>" . $name . "</li>";
}
?>
</ul>
You need to echo the array not the count which is $i. So your echo should look like this:
echo "<li>" . $names[$i] . "</li>";
This should work for you:
Just implode() your array, e.g.
echo "<li>" . implode("</li><li>", $names) . "</li>";
Your current code doesn't work, because your don't use your variable $i as index. So you would have to use:
$names[$i]
You need to pull the index out of the array, otherwise you will just get 0,1,2,3...
<ul>
<?php
$names = array('Mike', 'Chris', 'Jane', 'Bob');
for($i=0; $i < count($names); $i++) {
echo "<li>" . $names[$i] . "</li>";
}
?>
</ul>
Another way to code it would be:
<ul>
<?php $names = array('Mike', 'Chris', 'Jane', 'Bob'); ?>
<?php foreach ($names as $name) : ?>
<li><?php echo $name ?></li>
<?php endforeach ?>
</ul>
This question already has answers here:
Break PHP array into 3 columns
(7 answers)
Closed 7 years ago.
I have this array :
$result = array('description1', 'description2', 'description3', 'description4', 'description5'
I want to split this array into divs like this :
$result[0] - $result[1] => put these into a div
$result[2] - $result[3] => put these into a div
$result[4] => put this into a div
My entire structure
$content = get_the_content();
$description = array();
$j=0;
if (preg_match_all('/<div id="description" class="description">([^<]*)<\/div>/', $content, $match)) {
for( $i = 0; $i < count($match[0]); $i = $i+1 ) {
$description[] = $match[0][$i];
}
}
$attachments =& get_children($args);
$arrayMatches = array();
if ($attachments) {
foreach(array_chunk($attachments, 2) as $img) {
echo '<div class="two_cols">';
foreach($img as $attachment) {
foreach($attachment as $attachment_key => $attachment_value) {
$imageID = $attachment->ID;
$imageTitle = $attachment->post_title;
$imagearray = wp_get_attachment_image_src($attachment_value, $size, false);
$imageAlt = get_post_meta($imageID, '_wp_attachment_image_alt', true);
$imageURI = $imagearray[0]; // 0 is the URI
$imageWidth = $imagearray[1]; // 1 is the width
$imageHeight = $imagearray[2]; // 2 is the height
?>
<div class="col_1_2">
<!-- A picure Here -->
<?php $arrayMatches[] = $match[0][$j]; ?>
</div>
<?php
break;
}
$j++;
}
$arrayMatches = array_chunk($arrayMatches, 2);
echo "<div>";
foreach($arrayMatches as $v) {
echo implode($v);
}
echo "</div>";
echo '</div>';
}
}
This should work for you:
Just chunk your array with array_chunk(). And then you can simply loop through your array, output it into a div and implode() the elements.
<?php
$result = array('description1', 'description2', 'description3', 'description4', 'description5');
$result = array_chunk($result, 2);
foreach($result as $v) {
echo "<div>" . implode(" ", $v) . "</div>";
}
?>
output:
<div>description1 description2</div>
<div>description3 description4</div>
<div>description5</div>
EDIT:
As from your updated array structure just grab all values first like this:
$result = [];
$arr = array(['description1'], ['description2'], 'description3', 'description4', 'description5'); //example
array_walk_recursive($arr, function($v, $k)use(&$result){
$result[] = $v;
});
$result = array_chunk($result, 2);
Can you not just echo them in divs:
<div>
<?php echo $result[0] . "-" . $result[1]; ?>
</div>
<div>
<?php echo $result[2] . "-" . $result[3]; ?>
</div>
<div>
<?php echo $result[4]; ?>
</div>
You just need to control whether you are in the last 2 positions or not.
echo '<div>';
for ($i=0;$i<count($result);$i=$i+2) {
if ($i+1 >= count($result)) {
echo $result[$i];
} else {
echo $result[$i].$result[$i+1];
echo '</div><div>';
}
}
echo '</div>;
How to add in this code multiple ID category.
I have in kategorija_id 5 arrays:
1 - News
2 - Magazine
3 - Kokursi
4 - Grantovi ect..
I whant to display just key 1 and 2
when i add array i just print the first key
$kategorija = Kategorija::model()->findByPk($item->kategorija_**id=1,2**);
Thnx :)
Code update:
<div class="carousel-inner">
<div class="item active">
<ul class="thumbnails">
<?php
$i = 0;
$lastDefault = 0;
foreach ($ids as $itemId) {
$item = InfoPaket::model()->findByPk($itemId);
$kategorija = Kategorija::model()->findByPk($item->kategorija_id);
if ( $i % 3 == 0 && $i > 0 ) {
echo "</ul></div><div class='item'><ul class='thumbnails'>";
}
$imageUrl = $_SERVER['DOCUMENT_ROOT'] . Yii::app()->baseUrl . "/images/infopaket/" . $item->slika;
if ( !file_exists($imageUrl) || empty($item->slika)) {
if ( $lastDefault == 0 ) {
$imagePath = Yii::app()->baseUrl . "/images/infopaket/info_paket.png";
$lastDefault = 1;
} else {
$imagePath = Yii::app()->baseUrl . "/images/infopaket/info_paket_redv2.png";
$lastDefault = 0;
}
} else {
$imagePath = Yii::app()->baseUrl . "/images/infopaket/" . $item->slika;
}
echo "<li class='span3 thmb-elem'>";
echo "<div class='thumbnail right-caption image'>";
echo "<a class='img-link' href='" . Yii::app()->getHomeUrl() . "?r=infoPaket/clanak&id=" . $item->id . "'><img class='img-class' src='$imagePath'></a>";
echo "</div>";
echo "<div class='caption caption-link'>";
echo "<div class='naziv'><img class='arrow-img' src='". Yii::app()->getBaseUrl() . "/themes/shadow_dancer/images/arrow.png'> " . strtoupper($kategorija->naziv) . "</div>";
echo "<p><a class='naslov' href='" . Yii::app()->getHomeUrl() . "?r=infoPaket/clanak&id=" . $item->id . "'><strong>" . $item->naslov . "</strong></a></p>";
echo "</div>";
echo "</li>";
$i++;
}
?>
</ul>
</div>
</div>
I solve this problem adding this line of code
if ($item->kategorija_id==3 || $item->kategorija_id==4 ) {
now code look like this and it works perfect.
<?php
$lastDefault = 0;
foreach ($ids as $itemId) {
$item = InfoPaket::model()->findByPk($itemId);
$kategorija = Kategorija::model()->findByPk($item->kategorija_id);
if ($item->kategorija_id==3 || $item->kategorija_id==4 ) {
echo '<li>';
echo '<img src="<?php echo Yii::app()->theme->baseUrl; ?>/images/infopaket/info_paket.png" />';
echo '<h2>';
echo strtoupper($kategorija->naziv);
echo '</h2>';
echo '<h1>';
echo "<a class='naslov' href='" . Yii::app()->getHomeUrl() . "?r=infoPaket/clanak&id=" . $item->id . "'>";
echo $item->naslov;
echo '</h1>';
echo '</a>';
echo '</li>';
}
}
?>
findByPk() accepts first argument as array too. So you can do
category::model()->findByPk(array('1', '2'));
Also, $item->category_id will return only single entry. If your item has multiple categories, than set up relations to categories and then get categories ID's with
CHtml::listData($item->categoriesRelationName, 'id', 'id');
can use function ($data){return $data->attribtues} too ---------^--- to get all attributes of model
$menu = mysql_query(" query 1");
$k = 1;
for ($s = 0; $s < mysql_num_rows($menu); $s++)
{
$menus= mysql_fetch_assoc($menu);
$mainmenu[]=$menus['name'];
$submenus=mysql_query("query 2");
for ($m = 0; $m < mysql_num_rows($submenus); $m++)
{
$submenu = mysql_fetch_assoc($submenus);
$subitem[]=$submenu['name'];
$url=$submenu['url'];
}
}
foreach($mainmenu as $mains)
{
echo '<li class="hasul"><a><span><b>' .$mains.'</b></span></a></li>';
foreach($subitem as $sub)
{
echo '<ul>';
echo '<li><span>' .$sub. '</span></li>';
echo '</ul>';
}
}
The code above show two queries which load a menu and submenus. Query2 uses some inputs from query1.
The menus do load correctly but they contain same menu items. Ideally each menu should have its own menu items.
You have a foreach inside your foreach, that means, that for every main menu you are outputing all the submenus.
Create some parameter for submenu, like:
$mainmenu = array(
1 => 'about',
2 => 'news',
3 => 'search',
);
$submenu = array(
1 => array( 'about my name', 'about my location' ),
3 => array( 'search me' ),
);
And now check only the submenu you need:
if ( is_array( $mainmenu ) )
{
echo '<ul>';
foreach( $mainmenu as $key=>$menu )
{
echo '<li>'.$menu.'</li>';
if ( is_array( $submenu[$key]) )
{
echo '<ul>';
foreach( $submenu[$key] as $sub )
{
echo '<li>'.$sub.'</li>';
}
echo '</ul>';
}
}
echo '</ul>';
}
This will produce:
<ul>
<li>about</li>
<ul>
<li>about my name</li>
<li>about my location</li>
</ul>
<li>news</li>
<li>search</li>
<ul>
<li>search me</li>
</ul>
</ul>
I hope you get the idea.
use this.. this could be easier to understand..
echo '<ul>';
$qry = mysql_query("query 1");
while ($row = mysql_fetch_array($qry))
{
echo "<li>";
echo "<a href='" . $row['url'] . "'>'" . $row['name'] . "'</a>";
$qry2 = mysql_query("query 2");
if (mysql_num_rows($query2) > 0)
{
echo "<ul>";
while ($row2 = mysql_fetch_array($query2))
{
echo "<li>";
echo "<a href='" . $row2['url2'] . "'>'" . $row2['name2'] . "'</a>";
echo "</li>";
}
echo "</ul>";
}
echo "</li>";
}
echo "</ul>";
let me know if you want any further help...