I'm trying to resolve the undefined offset. I've Google and looked through stack overflow, but the examples I've found either didn't apply or were too complex for someone of my skill to fathom at this time. I'm very green, but I'd done diligence I promise before asking for help and wisdom.
This is the function as it now exists:
function PrintFolio($aaPh) //aaPh =associative array place holder
{
//print out X number rows of unto 4 images from array with X items
//if array had 7 items, print one row of 4 next row 3
//if array had 16 items, print 4 rows of 4, etc.
//if array had 13 items, print 3 rows of 4, final row with one item
$itemsCount = sizeof($aaPh);//get size of array
$height = (int)ceil(sizeof($aaPh)/4);//rounded up division by 4
//$height = $height + 1;
$keys = array_keys($aaPh); //get keys
//loop through array of X items, for each group of 4 print as a row
for($row = 0; $row < $height; $row++) //looping through the rows
{
echo '<div class="row flush">'; //open div
for($image = 0; $image < 4; $image++) //each row is composed of 4 images
{
$aaPhIndex = array_keys($aaPh)[$row*4+$image]; //if associative array
if( $row*4+$image < $itemsCount ) {
$aaPhIndex = $keys[$row*4+$image];
printf(TEMPLATE_PORTFOLIO, $aaPhIndex, $aaPh[$aaPhIndex]);
//$template = '<div class="3u"><img src="_img/thumbs/%1$s" alt="" title="%2$s" /></div>';
}
}
echo '</div>'; //end div group of 4
}//end loop
}//end function
It takes an array, slices it up in to units of four and then prints the array as a series of images to the screen. But if I don't have a number which is exactly devisable by 4, it will display whatever open slots remain as undefined offset errors (I hope I am saying that correctly).
My goal is to not have the undefined offset errors print to these screen without revising my site error reporting capabilities (I think doing that would be cheating as it wouldn't correct the problem, just hide it which doesn't seem very above board to me).
Why don't you just put a line break after each 4th element?
function PrintFolio($aaPh) //aaPh =associative array place holder
{
//loop through the array
for($row = 0; $row < Count($aaPh); $row++) //looping through the rows
{
if (!$row || !($row%4)) {
if ($row) echo '</div>'; // close previously opened divs
echo '<div class="row flush">'; //open div
}
/* show your picture code here */
}//end loop
echo '</div>';
}//end function
Here is an update after your comments.
Because you do not need values of your associative array, only keys, the code can be changed as follows:
function PrintFolio($aaPh) //aaPh =associative array place holder
{
//loop through the array
$row=0;
foreach(array_keys($aaPh) as $img) //looping through array keys only
{
if (!$row || !($row%4)) {
if ($row) echo '</div>'; // close previously opened divs
echo '<div class="row flush">'; //open div
}
echo '<img src="' . $img . '">';
$row++; // increase row counter.
}//end loop
echo '</div>';
}//end function
Make sure to put proper path into the tag
Since you can't assume that you're always going to have a number of elements that's exactly divisible by four, you need to test each one. In your inner loop where you calculate the index and then use it to refer to the array, just add a check to ensure that array element exists. From this:
$aaPhIndex = $keys[$row*4+$image];
printf(TEMPLATE_PORTFOLIO, $aaPhIndex, $aaPh[$aaPhIndex]);
To this:
$aaPhIndex = $keys[$row*4+$image];
if (isset($aaPh[$aaPhIndex])) {
printf(TEMPLATE_PORTFOLIO, $aaPhIndex, $aaPh[$aaPhIndex]);
}
The error in your logic is that on this line
for($image = 0; $image < 4; $image++) //each row is composed of 4 images
You assume there are always 4 images. But like you said yourself, if the array has for example 13 items, then the last row will only contain one image. This will cause $aaPhIndex = array_keys($aaPh)[$row*4+$image]; to access an array index that doesn't exist.
To solve the problem you will either have to modify $image < 4 to also account for the last row, or just check that the index doesn't exceed the item count. For example by placing the error line under the condition that you already wrote:
if( $row*4+$image < $itemsCount ) {
$aaPhIndex = array_keys($aaPh)[$row*4+$image]; //if associative array
$aaPhIndex = $keys[$row*4+$image];
printf(TEMPLATE_PORTFOLIO, $aaPhIndex, $aaPh[$aaPhIndex]);
//$template = '<div class="3u"><img src="_img/thumbs/%1$s" alt="" title="%2$s" /></div>';
}
Hope this works
Related
I have a database with "7" user account objects.
I have called the objects using any of the loop functions.
foreach (objects as object) {
echo '<div class="col-4>"'.object->property.'"</div>';
}
I want to add rows for each 3 columns as they are being displayed.
The above code would result in all my objects/colums/divs being nested in one row.
I want a way in which I can have the loop adding a row for every 3 columns.
Count the objects as you go. If $x divided by 3 has no remainder, close and open the next div:
$x = 0;
foreach ($objects as $object){
$x ++;
echo '<div class="col-4">"'.$object->property.'"</div>';
echo ($x % 3 == 0) ? '</div><div class="row">' : '';
}
I am facing the problem is that I want to display all image by using array and loop,it is not showing all image ,it does show only one image.Here is my code
if($objResult['Image_Type'] == '01') {
$img11 = array($objResult['Image_Name']);
for ($i=0; $i < count($img11[$i]); $i++) {
$im_mouths = array($img11[$i]);
}
}
here is in the database to extract out all image
the out put is showing only one image by using this code
<? echo $im_mouths[0].'11111'.'<br>';?>
<? echo $im_mouths[1].'222222'.'<br>';?>
<? echo $im_mouths[2].'333333'.'<br>';?>
I am not sure I'm doing correctly or not ,help me out this problem.
Thanks :)
It's only showing one image because you're re-writing $im_mouths each time you loop. You should change your loop to do the following:
$im_mouths = array();
if($objResult['Image_Type'] == '01') {
$img11 = array($objResult['Image_Name']);
for ($i=0; $i < count($img11[$i]); $i++) {
$im_mouths[] = array($img11[$i]);
}
}
We initialize $im_mouths as an array, then add each element to the array as an element: $im_mouths[] = ....
You need to simplify your code a bit.
Assuming you have a outer loop for retrieving data from database:
$im_mouths = array();
while ($objResult = $res->fetch()) {
if($objResult['Image_Type'] == '01') {
$im_mouths[] = $objResult['Image_Name'];
}
}
Now you have $im_mouths array of elements of image names, as many as you have in database. So as you see you don't need unnecessary inner for loop
Finally to see what you have in your $im_mouths array, you can print_r it:
print_r($im_mouths);
In your example, the variable $im_mouths should contain only the last image ( that is, 20140923jaobangsaen01.png ). Because you are using simple variable to store the image, so that the variable may contain last image while the program flow comes out from the for loop. So that the output of your example is correct. If you want to show each images means, you should use array variable to store all the images and display back to the required place. Rearrange the code like following,
$im_mouths = array();
if($objResult['Image_Type'] == '01') {
$img11 = array($objResult['Image_Name']);
for ($i=0; $i < count($img11[$i]); $i++) {
$im_mouths[] = array($img11[$i]);
}
}
Hope it will work fine for you.
Use can use foreach to display the images instead of using echo for each image.
$num = 1111; //if required
foreach ( $im_mouths as $value ) {
echo $value.$num;
$num += 1111;
}
I have a 2-dimentional array with 30+ item nodes in the following format.
$catalog= array(
array(
code => "ABC",
name => "Item name",
link => "domain.com/item121"
),
array(
code => "DEF",
name => "Another item name",
link => "domainB.com/item333"
)
);
I need to do the following:
Randomize the array
Display the first 5 items in a row
Display the rest inside another container 5 items per row.
I only want to show complete rows of 5 and no partials. So I count the total items:
$items= count($catalog);
Then I count how many to display to have complete rows of 5:
$showItems = floor($logosN / 5) * 5; // num or rows * cnt per row
I am not sure how to do the rest. I am able to output the items without randomizing
echo '<div class="first5">';
// 5 first items here
echo '</div>';
echo '<div class="restItems">';
// rest items need to go here
for ($x = 0; $x <= $showItems - 1; $x++) {
echo '
<div class="item">
<div class="item_'.$catalog[$x][code].'"></div>
</div>';
}
echo '</div>';
Need some help here. Thanks.
I had a look at the examples posted by the users on the PHP documentation for shuffle and found this function, which if I understand your question correctly does what you want:
Reference: http://www.php.net/manual/en/function.shuffle.php#93214
<?php
function twodshuffle($array)
{
// Get array length
$count = count($array);
// Create a range of indicies
$indi = range(0,$count-1);
// Randomize indicies array
shuffle($indi);
// Initialize new array
$newarray = array($count);
// Holds current index
$i = 0;
// Shuffle multidimensional array
foreach ($indi as $index)
{
$newarray[$i] = $array[$index];
$i++;
}
return $newarray;
}
?>
Please note it only works on two dimensional arrays.
Also, note that it will not shuffle the actual inner array elements, but only shuffle the outer array, if you know what I mean, and if I know what you want ;)
im trying to figure out how to create new DIV every 8 MySQL rows queried. I am integrating jPagination into my site and so I need to create a new DIV container every 8 rows it receives from the database. Any ideas?
you need this: %
not sure about how your code exactly goes, but in the loop of every row you make a count++ and then something like this which would be in C:
if(!count%8) {
print DIV eccc
}
just so you understand what this % does: it gives you the remainer of a division. For example, if your row is number 20, so count equals to 20 at that moment, 20%8 will equal 4. That is because if you divide 20/8 you will have 2.** something, then multiply 2*8 you get 16. Take 20-16 = 4. So 20%8 is 4. Only when the number in count++ is perfectly divisible by number 8 you will get 0 zero there. So your IF statement says: if there ia no remain dividing count by 8 then do this
maxim
i = 1
while( gettingRows )
{
doWhateverYouDoHere()
if ( i%8 === 0 )
print "div"
++i
// or put increment right into the if statement like ( if i++ % 8 === 0 )
}
<?php
// previous code.....
$counter = 1;
echo '<div class="outercssclass">';
echo '<div class="innercssclass">';
// fetch mysql query data into $results....
// you can do validations with mysql_num_rows to check the number of rows the query returned
foreach($results as $result) {
$counter++;
if($counter % 8 == 0) {
echo '</div><div class="innercssclass">';
}
// other logic...
// rest of the code
}
echo '</div>'; // for closing the inner div
echo '</div>'; // for closing the outer wrapper div
// rest of the program logic...
Im using wordpress as my base and I need to output the post meta for all my posts, but only for a certain number of keys.
What I intend to do is to save a list of all my keys, that im going to use to query wordpress for metadata for that post.
Below is my code.
//HERE YOU CAN SEE THE KEYS IMM USING ATM
$nyckellista[] = array("ebutik_allm_bas_operativsystem" ,"--foretagsform"
,"ebutik_allm_bas_omsättning");
$i = 0;
//Here im trying to query the get_post_meta with my keys and save the result (it's an array aof values that it return)
foreach($nyckellista as $nyckel)
{
$nyckellista[$i] = get_post_meta($post->ID,$nyckel,false);
echo $i . "Nyckel:" . $nyckel[$i];
$i++;
}
//HERE ME TRYING TO PRINT THE ARRAY CONTENTS
$count = count ($nyckellista);
echo $count;
for($y=1; $y <= $count; $y++)
{
$countmore=count($nyckellista[$y]);
for($x=1; $x <= $countmore; $x++)
{
print ($nyckellista[$y][$x] . "<br> ");
}
echo "<br>";
}
WHAT AM I DOING WRONG?
In the first line the $nyckellista variable is being declared implicitly as an array and then you're assigning to its first position an array of values.
Is this what you need/intend?
Edit:
Another point is, in the counts area of the code, that php arrays get numeric indexes starting at 0, not at 1 (see Example #4 in PHP array reference)