I am trying to output the results of some blog posts using a foreach loop but I keep getting the error "Undefined index: title"
<?php foreach ($post as $post_item):?>
<div class="postDiv">
<h4><?php echo $post_item['title'];?></h4>
<p><?php echo $post_item['summary'];?></p>
</div>
<?php endforeach;?>
here is my array when calling var_dump($post)
array(4) {
["id"]=> array(5) {
[0]=> string(2) "15"
[1]=> string(2) "16"
[2]=> string(2) "17"
[3]=> string(2) "18"
[4]=> string(2) "19" }
["title"]=> array(5) {
[0]=> string(3) "234"
[1]=> string(2) "11"
[2]=> string(1) "2"
[3]=> string(3) "444"
[4]=> string(5) "title" }
["summary"]=> array(5) {
[0]=> string(3) "213"
[1]=> string(2) "11"
[2]=> string(1) "2"
[3]=> string(3) "444"
[4]=> string(7) "summary" }
["content"]=> array(5) {
[0]=> string(3) "234"
[1]=> string(1) "1"
[2]=> string(1) "2"
[3]=> string(3) "444"
[4]=> string(7) "content" } }
I am a noob at php and I am also using codeigniter if that makes a difference here (I dont think it should). If I use the following code I can print out the posts for the title only but this is not what I want as I want to add in more fields later
<?php foreach ($post['title] as $post_item):?>
<div class="postDiv">
<h4><?php echo $post_item;?></h4>
</div>
<?php endforeach;?>
Here is my final code that worked for me
<?php
foreach (array_reverse(array_keys($post["id"]), true) as $key):?>
<div class="postDiv">
<h4><?php echo $post['title'][$key];?></h4>
<p><?php echo $post['summary'][$key];?></p>
<p><?php echo $post['content'][$key];?></p>
<div style="float:right">
<p><?php echo anchor('admin/edit/'.$post['id'][$key],' [edit]');?></p>
<p><?php echo anchor('admin/delete/'.$post['id'][$key],' [delete]');?></p>
</div>
</div>
<?php endforeach;?>
Your array is somehow misaligned for your design.
<?php foreach (array_keys($post["id"]) as $key):?>
<div class="postDiv">
<h4><?php echo $post['title'][$key];?></h4>
<p><?php echo $post['summary'][$key];?></p>
</div>
<?php endforeach;?>
It looks like your post information is split across multiple array keys based on a numeric reference, so you can use that numeric key reference to access the sibling array keys which contain the rest of your data.
foreach($post['id'] as $key => $id) {
$title = $post['title'][$key];
$summary = $post['summary'][$key];
$content = $post['content'][$key];
}
Just for you understanding:
with foreach ($a as $k) you are iterating an array $a, and putting in each iteration, one first level element in $k.
So, in the first iteration,
$post_item will be
array(5) {
[0]=> string(2) "15"
[1]=> string(2) "16"
[2]=> string(2) "17"
[3]=> string(2) "18"
[4]=> string(2) "19" }
in the second, $post_item will be:
array(5) {
[0]=> string(3) "234"
[1]=> string(2) "11"
[2]=> string(1) "2"
[3]=> string(3) "444"
[4]=> string(5) "title" }
and so on.
I dont even think that this secodn level indexes, 0,1,2,3,4,5 has an specific meannig to you, and for sure, if you do that foreach( array_keys($post["id"]) ...) would be the same as just iterating from harcoded 0 to count($post), and if you iterate foreach( $post["id"] as $k) and then use $k as index to the other elements, that wouldnt work alos, since the values of id, are not the keys of any other elements. so you will get many undefined index warnings.
and also im not sure if the values, the right side of every element, are actually any kind of usefull information for you. So, we could find some tricks here to get the result that you want, but what you need to fix is one step before, when you create $post
Related
I have a CMS I am using that serializes their data in the database. I used the unserialize() function to convert the data into an associative array. Now I am having a hard time pulling the value of the image from the associative array:
This is the simple while loop I am using to loop through the rows:
while($row = mysql_fetch_assoc($query_models)){
$model_name = $row['ModelName'];
$model_thumbnail = unserialize($row['info']);
}
this is the Key and Value of the array I need to get the value of, so I can assign the correct thumbnail image to the respected person:
["1x_filename"]=> string(19) "00/83/83-set-1x.jpg"
The full array is below, and the key I am targeting is located more at the bottom of this array:
array(1) {
["thumbs"]=> array(2) {
[16]=> array(17) {
["id"]=> string(2) "82"
["1x_width"]=> string(3) "220"
["1x_height"]=> string(3) "330"
["2x_width"]=> string(3) "440"
["2x_height"]=> string(3) "660"
["3x_width"]=> string(3) "660"
["3x_height"]=> string(3) "990"
["4x_width"]=> string(3) "880"
["4x_height"]=> string(4) "1320"
["width"]=> string(3) "220"
["height"]=> string(3) "330"
["retinamode"]=> string(1) "1"
["filename"]=> string(10) "82-set.jpg"
["1x_filename"]=> string(19) "00/82/82-set-1x.jpg"
["2x_filename"]=> string(19) "00/82/82-set-2x.jpg"
["3x_filename"]=> string(19) "00/82/82-set-3x.jpg"
["4x_filename"]=> string(19) "00/82/82-set-4x.jpg"
}
[17]=> array(17) {
["id"]=> string(2) "83"
["1x_width"]=> string(3) "106"
["1x_height"]=> string(3) "150"
["2x_width"]=> string(3) "212"
["2x_height"]=> string(3) "300"
["3x_width"]=> string(3) "318"
["3x_height"]=> string(3) "450"
["4x_width"]=> string(3) "424"
["4x_height"]=> string(3) "600"
["width"]=> string(3) "106"
["height"]=> string(3) "150"
["retinamode"]=> string(1) "1"
["filename"]=> string(10) "83-set.jpg"
["1x_filename"]=> string(19) "00/83/83-set-1x.jpg"
["2x_filename"]=> string(19) "00/83/83-set-2x.jpg"
["3x_filename"]=> string(19) "00/83/83-set-3x.jpg"
["4x_filename"]=> string(19) "00/83/83-set-4x.jpg"
}
}
}
Any help would be greatly appreciated. Thanks!
Just like this :
$model_thumbnail = unserialize($row['info']);
$picturePath = $model_thumbnail['thumbs'][17]['1x_filename'];
picturePath will contain your images path
I would recommend you to use mysqli or an other database adapter in php, as mysql_ functions are depricated and even removed in never php versions. But according to your code, you could simply use a foreach loop to get your data.
Your results gives back some kind of data groups, I assume those are the user ids or something similar. It could also be the version or something like it. As I do not know that here are two possible ways
Get all:
while($row = mysql_fetch_assoc($query_models)){
$model_name = $row['ModelName'];
$model_thumbnail = unserialize($row['info']);
// echo all 1x_filename values from all given "ids"
foreach ($model_thumbnail['thumbs'] as $model_id => $model_values) {
print $model_values['1x_filename'];
}
}
Get only the latest id, in case those are for some kind of versioning:
while($row = mysql_fetch_assoc($query_models)){
$model_name = $row['ModelName'];
$model_thumbnail = unserialize($row['info']);
// sort array, so highest ID (assume-ably the newest)
krsort($model_thumbnail['thumbs']);
// get array entry
$firstEntry = reset($model_thumbnail['thumbs']);
print $firstEntry['1x_filename'];
}
It's hard to explain it with words, but what I want to do is skip the ranking when there is no ranking information in CakePhp2. For example, I have the following data that contains 7 rows of ranking data. 2 out of the 7 row contains the empty body(2nd and the 5th row). So I made a script to skip the ranking post that is empty. However, Since the 2nd and the 5th rows that have been skipped. The data post that is displayed is 1,3,4,6,7. But I want to the display the ranking like 1,2,3,4,5. In other words, I want to show the 3rd ranking as 2nd and 4th as 3rd and so forth. Sorry for my poor explanation. Simply, I want to skip ranking when there is no ranking information and show the ranking number. (PS: I don't want to alter the DB) I will love to hear from you!
array(7) { [0]=> array(1) { ["Post"]=> array(5) { ["id"]=> string(1) "1" ["title"]=> string(6) "title1" ["body"]=> string(5) "body1" ["created"]=> string(19) "2017-04-04 21:25:43" ["modified"]=> NULL } } [1]=> array(1) { ["Post"]=> array(5) { ["id"]=> string(1) "2" ["title"]=> string(0) "" ["body"]=> string(0) "" ["created"]=> string(19) "2017-04-04 21:25:43" ["modified"]=> NULL } } [2]=> array(1) { ["Post"]=> array(5) { ["id"]=> string(1) "3" ["title"]=> string(6) "title3" ["body"]=> string(5) "body3" ["created"]=> string(19) "2017-04-04 21:25:43" ["modified"]=> NULL } } [3]=> array(1) { ["Post"]=> array(5) { ["id"]=> string(1) "4" ["title"]=> string(6) "title4" ["body"]=> string(5) "body4" ["created"]=> string(19) "2017-04-08 15:48:21" ["modified"]=> NULL } } [4]=> array(1) { ["Post"]=> array(5) { ["id"]=> string(1) "5" ["title"]=> string(0) "" ["body"]=> string(0) "" ["created"]=> string(19) "2017-04-08 16:14:08" ["modified"]=> NULL } } [5]=> array(1) { ["Post"]=> array(5) { ["id"]=> string(1) "6" ["title"]=> string(6) "title6" ["body"]=> string(5) "body6" ["created"]=> string(19) "2017-04-08 16:14:08" ["modified"]=> NULL } } [6]=> array(1) { ["Post"]=> array(5) { ["id"]=> string(1) "7" ["title"]=> string(6) "title7" ["body"]=> string(5) "body7" ["created"]=> string(19) "2017-04-08 16:14:08" ["modified"]=> NULL } } }
Trying to make a script to show the Popular ranking. I really love to hear about some great Hints and samples from you!
<h1>Popular Ranking posts</h1>
<?php $k = 1; ?>
<?php for($i = 0; $i <= count($posts); $i++) { ?>
<ul>
<?php if (!empty($posts[$i]['Post']['body'])) { ?>
<h2><?php echo $posts[$i]['Post']['title']; ?></h2>
<li>
<?php
echo $posts[$i]['Post']['body'];
?>
</li>
//Want to show the number 2 in $k even though the 2nd body data is missing(Currently 3rd data).
<h3 class="ranking_number"><?php echo $k; ?></h3>
<?php } else {
continue;
}?>
</ul>
<?php $k++; } ?>
<h1>Popular Ranking posts</h1>
<?php $k = 1; ?>
<?php for($i = 0; $i <= count($posts); $i++) { ?>
<ul>
<?php if (!empty($posts[$i]['Post']['body'])) { ?>
<h2><?php echo $posts[$i]['Post']['title']; ?></h2>
<li>
<?php
echo $posts[$i]['Post']['body'];
?>
</li>
//Want to show the number 2 in $k even though the 2nd body data is missing(Currently 3rd data).
<h3 class="ranking_number"><?php echo $k; ?></h3>
<?php $k++; } ?>
<?php } else {
continue;
}?>
</ul>
What was changed?
I moved $k++; inside of if, just before else
Why?
What was your code doing:
Set $k to 1.
Post 1 isn't empty, so show it (rank 1).
Increase $k by 1.
Go to next post
Post 2 is empty, so don't show it (rank 2).
Increase $k by 1.
Let's stop there. Your $k was increased with every post, even those skipped, so the third position was 3rd instead of 2nd.
What is my code doing:
Set $k to 1.
Post 1 isn't empty, so show it (rank 1).
Increase $k by 1.
Go to next post
Post 2 is empty, so don't show it (rank 2).
Go to next post
Post 3 isn't empty, so show it (still rank 2).
I hope I helped.
This question already has answers here:
Shuffle an array in PHP
(6 answers)
Closed 9 years ago.
I'm trying to make an image gallery. On the index file I want to show the albums with the images, see; http://www.robcnossen.nl/
I want to randomize the images that are inside these albums but I get all sorts of errors like:
warning: rand() expects parameter 1 to be long, string given in.
My code is;
foreach ($albums as $album) {
?><div><h2><?php
echo'',$album['name'], '';?> </h2><?php
echo'<img src="uploads/thumbs/', $album["id"], '/', $album["imagename"],'" title="" />';
?></div><?php
}
The $album["imagename"] are the images inside the albums and I want to randomize this part. I tried for example:
rand($album["imagename"], 0)
but that gives an error.
I also tried shuffle;
foreach ($albums as $album) {
shuffle($album["imagename"]);
?><div><h2><?php
echo'',$album['name'], '';?></h2><?php
echo'<img src="uploads/thumbs/', $album["id"], '/', $album["imagename"],'" title="" />';
?></div><?php
}
But also there I get only errors.
Can anybody help me with this?
var_dump($albums);
gives
array(2) {
[0]=> array(8) {
["id"]=> string(1) "8"
["timestamp"]=> string(10) "1373890251"
["name"]=> string(7) "Holland"
["description"]=> string(19) "Fantastische foto's"
["count"]=> string(1) "2"
["imagename"]=> string(38) "KONICA MINOLTA DIGITAL CAMERA_428.jpeg"
["image"]=> string(2) "63"
["ext"]=> string(0) ""
}
[1]=> array(8) {
["id"]=> string(1) "9"
["timestamp"]=> string(10) "1376914749"
["name"]=> string(6) "Belgie"
["description"]=> string(11) "Mooi Belgie"
["count"]=> string(1) "2"
["imagename"]=> string(12) "PICT0170.JPG"
["image"]=> string(2) "66"
["ext"]=> string(0) ""
}
}
as result.
shuffle should work for you, but not if you put it inside the foreach loop, like your example code. You need to shuffle before you start the loop. Also, shuffle needs to be called with the array itself, not an item of the array:
shuffle($albums);
foreach ($albums as $album) {
...
}
you should shuffle array out side the loop
<?php
shuffle($albums);
foreach ($albums as $album)
{
?><div><h2>
<?php
echo'',$album['name'], '';
?>
</h2>
<?php
echo'<img src="uploads/thumbs/', $album["id"], '/', $album["imagename"],'" title="" />';
?>
</div>
<?php
}
I am noticing some strange behavior while looping through some data. I'm sure it's something simple, but I can't seem to be able to find the bug.
I have the following logic:
<?php
print 'dumping data : <BR>';
var_dump($portvlan);
print '<BR>';
print 'looping through data: <BR>';
foreach ($portvlan as $vlandetail){
echo 'Vlanid: '.$vlandetail['VlanId'].'<BR>';
echo 'Name: '.$vlandetail['Name'].'<BR>';
echo 'Mode: '.$vlandetail['Mode'].'<BR>';
}
?>
This is the output that I'm getting:
dumping data :
array(3) { ["VlanId"]=> string(2) "33" ["Name"]=> string(6) "USR_33" ["Mode"]=> string(6) "Access" }
looping through data:
Vlanid: 3
Name: 3
Mode: 3
Vlanid: U
Name: U
Mode: U
Vlanid: A
Name: A
Mode: A
What I was expecting was to see it print a row with 3 cells, with the following values:
33, USR_33, Access.
Can you tell me where I'm going wrong?
Thanks.
EDIT 1
This logic works fine when the $portvlan array has more than one entry.
For example, on another set of data, the var_dump gives this result:
array(6) { [0]=> array(3) { ["VlanId"]=> string(1) "1" ["Name"]=> string(1) "1" ["Mode"]=> string(5) "Trunk" } [1]=> array(3) { ["VlanId"]=> int(2) ["Name"]=> int(2) ["Mode"]=> string(5) "Trunk" } [2]=> array(3) { ["VlanId"]=> int(3) ["Name"]=> int(3) ["Mode"]=> string(5) "Trunk" } [3]=> array(3) { ["VlanId"]=> int(4) ["Name"]=> int(4) ["Mode"]=> string(5) "Trunk" } [4]=> array(3) { ["VlanId"]=> int(5) ["Name"]=> int(5) ["Mode"]=> string(5) "Trunk" } [5]=> array(3) { ["VlanId"]=> string(2) "33" ["Name"]=> string(2) "33" ["Mode"]=> string(5) "Trunk" } }
And the loop logic works fine.
$vlandetail will be populated with a different item from the array on every iteration of the loop. You shouldn't be treating $vlandetail as an array. Just use it directly.
To get the key name of the array entry, you have to change the loop structure to this:
foreach ($portvlan as $key => $vlandetail) {
echo $key . ': ' . $vlandetail . '<br>';
}
How you get your $portvlan?
If you can know is $portvlan is not an array of $vlandetail, you can simply do this,
$portvlan = array($portvlan);
// start loop
Or you can do this before you loop it, to make sure $portvlan is a numerical array formed by $vlandetail
$portvlan = (isset($portvlan[0]))?$portvlan:array($portvlan);
// start loop
I have a SESSION that looks like this:
array(1) { [1]=> array(11) { ["aantal"]=> int(1) ["id"]=> string(2) "29" ["filmtitel"]=> string(16) "2_fast_2_furious" ["film_id"]=> string(1) "1" ["zaal_id"]=> string(1) "1" ["zaaltitel"]=> string(6) "zaal 1" ["tijdstip"]=> string(8) "17:30:00" ["stoeltjes"]=> array(3) { [0]=> string(2) "19" [1]=> string(2) "20" [2]=> string(2) "21" } ["dag"]=> string(8) "woensdag" ["verwijder"]=> int(1) ["vertoningId"]=> string(2) "31" } }
so there is an array and within that array is another array called "stoeltjes" with 3 items.
What i would like to know is how i can direct the content of "stoeltjes" to jquery so i can assign it to a flashvar and send it to as3.
Anyone who can help?
Take a look at json_encode if you have php 5.2 or higher. The resulting variable from that function can be set as a javascript variable.
I think it's something like this:
echo "
<script>
var stoeltjes = eval(".json_encode($_SESSION['stoeltjes']).");
</script>
";
Now 'stoeltjes' is a array with the same values but then in javascript.