php foreach loop issue in IE11 - php

I have very weird issue with php foreach loop in IE 11. In FF, Chrome, Opera, Safari, IE 7 8 9 10 Foreach loop works fine.
I used for() loop for dummy text it works fine in IE 11. But i don't know how to call arrays in for or while loop
here is my code.
$img_num4 = 4;
$data_chunks = array_chunk($wp_images, $num_img);
echo '<ul id="id1">';
foreach ($data_chunks as $data_chunk) {
echo '<li class="id2">';
foreach($data_chunk as $data) {
if($data['image_links_to'])
echo '<a href="'.$data['image_links_to'].'" '.$new_window.'>';
echo '<img src="'.$data['file_url'].'" class="logo-img" alt="" />';
if($data['image_links_to'])
echo '</a>';
}
echo '</li>';
}
echo '</ul>';
I don't know why foreach loop not working in IE 11 only. Any Suggestions.
I also try this:
$img_num4 = 4;
$data_chunks = array_chunk($wp_images, $num_img);
echo '<ul id="id1">';
for($i=1; $i<=10; $i++){
echo '<li>'.$i.'</li>';
}
/*foreach ($data_chunks as $data_chunk) {
echo '<li class="slide">';
foreach($data_chunk as $data) {
if($data['image_links_to'])
echo '<a href="'.$data['image_links_to'].'" '.$new_window.'>';
echo '<img src="'.$data['file_url'].'" class="logo-img" alt="" />';
if($data['image_links_to'])
echo '</a>';
}
echo '</li>';
}*/
echo '</ul>';
It shows me correct data in IE 11. But foreach loop not work
please tell me how can i call arrays in for or while loop

With a for loop it should be like :
$img_num4 = 4;
$data_chunks = array_chunk($wp_images, $num_img);
echo '<ul id="id1">';
$len = $data_chunks.lenght;
for ($i = 0; $i < $len; $i++) {
$data_chunk = $data_chunks[$i];
echo '<li class="id2">';
$len2 = $data_chunk.lenght;
for ($j = 0; $j < $len2; $j++) {
$data = $data_chunk[$j];
if($data['image_links_to']) {
echo '<a href="'.$data['image_links_to'].'" '.$new_window.'>';
}
echo '<img src="'.$data['file_url'].'" class="logo-img" alt="" />';
if($data['image_links_to']) {
echo '</a>';
}
}
echo '</li>';
}
echo '</ul>';
a while loop is not so useful here.
As metionned in comments, php's an html preprocessor so it does not need any browser to work. Maybe you should look on the html source in your IE11 browser to see what is buggy.
MAYBE (and that's an important part of the sentence) IE11 doesn't display <li> if it does not contains any text. Here you only put an <img> (sometimes with a link).
You should try replacing img tag with text in your foreach loop.
Apolo

Related

PHP Loop on every value on multidimensional array X times [duplicate]

I have a foreach statement in my app that echos a list of my database results:
<?php
foreach($featured_projects as $fp) {
echo '<div class="result">';
echo $fp['project_name'];
echo '</div>';
}
?>
I would like to:
On every third result, give the div a different class. How can I achieve this?
You can use a counter and the modulo/modulus operator as per below:
<?php
// control variable
$counter = 0;
foreach($featured_projects as $fp) {
// reset the variable
$class = '';
// on every third result, set the variable value
if(++$counter % 3 === 0) {
$class = ' third';
}
// your code with the variable that holds the desirable CSS class name
echo '<div class="result' . $class . '">';
echo $fp['project_name'];
echo '</div>';
}
?>
<?php
foreach ($featured_projects as $i => $fp) {
echo '<div class="result' . ($i % 3 === 0 ? ' third' : '') . '">';
echo $fp['project_name'];
echo '</div>';
}
?>
If the $featured_projects array is based on incremental index you could simply use the index and the modulo % operator.
Otherwise you would have to add a counter.
http://php.net/manual/en/language.operators.arithmetic.php
add a counter in this loop and check if counter equals three and apply class.
Using a counter and modulo operator this is easy to implement
<?php
foreach($featured_projects as $fp) {
if(++$i % 3 === 0) {
$class = ' something';
} else {
$class = '';
}
echo '<div class="result' . $class . '">';
echo $fp['project_name'];
echo '</div>';
}
?>
<?php
$i = 0;
foreach($featured_projects as $fp) {
echo '<div class="'.($i++%3 ? 'result' : 'other_class').'">';
echo $fp['project_name'];
echo '</div>';
}
?>
What leaves your code mostly in tact would be
<?php
$i = 1;
foreach($featured_projects as $fp) {
printf ('<div class="%s">',(($i % 3) ? "result" : "result_every_third" ));
echo $fp['project_name'];
echo '</div>';
$i++;
}
?>
But you may want to consider using a for or while construct around "each($featured_projects)" (see http://php.net/manual/en/function.each.php) which may result in neater code.
<?php
$counter = 0;
foreach ($featured_projects as $fp) {
echo '<div class="result' . ($counter++ % 3 === 0 ? ' third' : '') . '">';
echo $fp['project_name'];
echo '</div>';
}
?>
You can add a counter in loop ...try the following...
<?php
$i = 0;
foreach($featured_projects as $fp) {
$i = ++$i;
if(($i%3) == 0)
{
$class1 = 'test1';
}
else
{
$class1 = 'test2';
}
echo '<div class="'.$class1.'">';
echo $fp['project_name'];
echo '</div>';
}
?>
This is the working version, sorry for my prev version:
<?php
$featured_projects[0]['project_name'] = "pippo";
$featured_projects[1]['project_name'] = "pippo2";
$featured_projects[2]['project_name'] = "pippo3";
$class[0] = "class1";
$class[1] = "class2";
$i=0;
foreach($featured_projects as $fp) {
$i++;
if ($i == 3) {
$c = $class[1];
$i=0;
} else {
$c = $class[0];
}
echo "<div class=\"$c\">";
echo $fp['project_name'];
echo "</div>\n";
}
?>
Produces:
<div class="class1">pippo</div>
<div class="class1">pippo2</div>
<div class="class2">pippo3</div>

Why does this return empty strings?

I want to access the values of an associative array in PHP. I populate the array using the following loop in PHP:
$db = array("a","b","c");
foreach ($db as $q) {
$$q = 'value';
}
This version prints the correct values
foreach ($db as $q) {
echo '<li>'; echo $$q; echo '</li>';
}
\\THIS GIVES ME THE CORRECT OUTPUT <li>value</li><li>value</li><li>value</li>
But I want to access the values through their index
$num = count($db);
for ($i = 0; $i < $num; $i++) {
echo '<li>'; echo $$db[$i]; echo '</li>';
}
\\\\THIS GIVES ME THE WRONG OUTPUT (EMPTY STRINGS <li></li><li></li><li></li>
What is going wrong in the second version? How can I access the values in this associative array through an index correctly?
echo '<li>'; echo $$db[$i]; echo '</li>';
In this line is one $ too much. Write:
echo '<li>'; echo $db[$i]; echo '</li>';
This should do the trick.
PS: You don't have to write echo everytime. Use string concatenation:
echo '<li>' . $db[$i] . '</li>';
You are trying to do something quite strange, anyway the solution to your problem are braces: { }
$num = count($db);
for ($i = 0; $i < $num; $i++) {
echo '<li>'; echo ${$db[$i]}; echo '</li>';
}
Look how braces are resolving the ambiguity, since without them, php wouldn't know if you were referring to ${$db}[$i] or ${$db[$i]}

start end div after some elements php

I cannot solve problem with starting ending divs after couple of elements from array.
What i want to get is something like that:
<div>
element1
element2
element3
element4
</div>
<div>
element5
element6
element7
element8
</div>
<div>
element9
element10
</div>
Here is my php code:
$array = array("element1","element2","element3","element4","element5","element6","element7","element8","element9","element10");
$perRow = 4;
$count = 1;
foreach ($array as $arr){
// here div needs to start, use 4 elements from array and close
if($count % $perRow == 0 OR $count == 1){
echo '<div>';
}
echo $arr . '<br>';
// here should div close
$count++;
}
Try something like this
$array = array("element1","element2","element3","element4","element5","element6","element7","element8","element9","element10");
$perRow = 4;
$count = 0;
echo '<div>';
foreach ($array as $arr){
// here div needs to start, use 4 elements from array and close
if($count % $perRow == 0 && $count!=0){
echo '</div><div>';
}
echo $arr . '<br>';
// here should div close
$count++;
}
echo '</div>';
Okay I am not familiar with arrays and maybe something like this would work:
$array = array("element1","element2","element3","element4","element5","element6","element7","element8","element9","element10");
$i=0;
echo '<div>'
if (i<3) {
echo '$array[$i]';
$i++;
}
echo '</div>';
echo '<div>';
if ($i>3 && $i<7) {
echo '$array[$i]';
$i++;
}
echo '</div>';
echo '<div>';
if ($i>7 && $i<10) {
echo '$array[$i]';
$i++;
}
echo '</div>';

Pagination for SESSION Arrays Won't Stop After Arrays are Finished

I have a PHP Page that displays all of my SESSION's arrays using pagination. The pagination displays ten arrays a page. Currently, my session is holding eleven arrays. My problem is that when I go to the second pagination page which contains my eleventh array, it keeps displaying more arrays that are empty. The pagination keeps going on and on, until a million probably. I would like it to end right at the eleventh array preferably. If for example there was twelve arrays I would like it to end and stop paginating at the twelfth array. Here is an image of my problem:
Here is my full PHP page code:
<?
session_start();//start session for this page
include_once("config.php");
//instantiate variables
$currentpage = isset($_GET['pagenum']) ? (integer)$_GET['pagenum'] : 0;
$numperpage = 10; //number of records per page
$numofpages = count($_SESSION['products'])/$numperpage; //total num of pages
$first = 0; //first page
$last = $numofpages;
if($currentpage==0){ //which page is previous
$previous = 0; //if very first page then previous is same page
}else{
$previous = $currentpage-1;
}
if($currentpage==$last-1){//which page is last
$next = $currentpage;
}else{
$next = $currentpage+1;
}
echo '<form method="post" action="PAYMENT-GATEWAY">';
echo '<ul>';
$cart_items = 0;
$cart_itm = $_SESSION['products'];
for($x=($currentpage*10);$x<(($currentpage*10)+10);$x++){ //output data
$product_code = $cart_itm[$x]["code"];
$queryy = "SELECT TOP 1 product_name,product_desc, price FROM products WHERE product_code='$product_code'";
$results = mssql_query($queryy, $mysqli);
$obj = mssql_fetch_object($results);
if ($obj) {
echo ($x+1)." ".$cart_itm[$x]["code"]."<br>";
echo '<li class="cart-itm">';
echo '<span class="remove-itm">×</span>';
echo '<div class="p-price">'.$currency.$obj->price.'</div>';
echo '<div class="product-info">';
echo '<h3>'.$obj->product_name.' (Code :'.$product_code.')</h3> ';
echo '<div class="p-qty">Qty : '.$cart_itm[$x]["qty"].'</div>';
echo '<div>'.$obj->product_desc.'</div>';
echo '</div>';
echo '</li>';
$subtotal = ($cart_itm[$x]["price"]*$cart_itm[$x]["qty"]);
$total = ($total + $subtotal);
echo '<input type="hidden" name="item_name['.$cart_items.']" value="'.$obj->product_name.'" />';
echo '<input type="hidden" name="item_code['.$cart_items.']" value="'.$product_code.'" />';
echo '<input type="hidden" name="item_desc['.$cart_items.']" value="'.$obj->product_desc.'" />';
echo '<input type="hidden" name="item_qty['.$cart_items.']" value="'.$cart_itm[$x]["qty"].'" />';
$cart_items ++;
} else {
break; //if no more results, break the while loop
}
}
echo '</ul>';
echo '<span class="check-out-txt">';
echo '<strong>Total : '.$currency.$total.'</strong> ';
echo '</span>';
echo '</form>';
echo 'Checkout';
echo "<a href='page.php?pagenum=".$previous."'>Previous</a> <a href='page.php?pagenum=".$next."'>Next</a>"; //hyperlinks controls
?>
And here is my config.php page's full code:
<?php
$mysqli = mssql_connect('gdm','Ga','Rda!');
$objConnectee = mssql_select_db('Gdg',$mysqli );
?>
If anyone can tell me how I can fix this problem it would be greatly appreciated. Thank you for any help.
mssql_fetch_object returns FALSE if there are no more results, so you could stop execution with an if statement:
$obj = mssql_fetch_object($results);
if ($obj) {
echo '<li class="cart-itm">';
//etc... as before all the way down to
$cart_items ++;
} else {
break; //if no more results, break the for loop
}

how to increment CSS ID using PHP programmatically

how can i increment my css ID using php programmatically?
foreach ($query_cat->result() as $row_cat)
{
$row_cat_id = $row_cat->id;
echo '<div class="product-wrapper">';
echo '<div id="product-header" class="product-header">';
echo $row_cat->title;
echo '</div>';
echo '</div>';
$query_prod = $this->db->get_where('products', array('category_id'=>$row_cat_id));
foreach ($query_prod->result() as $row_prod)
{
$row_prod_id = $row_prod->id;
echo '<div id="product-contents" class="product-contents">';
echo $row_prod->title.' '.$row_prod->id;
echo '</div>';
}
}
what i want to happen is to increment the id product-header and product-contents depending on the numbers of rows generated
something like this
product-header1, product-header2, product-header3....
product-contents1, product-contents2, product-contents3....
thanks!
Just initialise an incremental variable before the foreach.
$i = $j = 0;
foreach ($query_cat->result() as $row_cat) {
$i++;
$row_cat_id = $row_cat->id;
echo '<div class="product-wrapper">';
echo '<div id="product-header'.$i.'" class="product-header">';
...
foreach ($query_prod->result() as $row_prod) {
$j++;
$row_prod_id = $row_prod->id;
echo '<div id="product-contents'.$j.'" class="product-contents">';
...
}
}
$i = $j = 1;
foreach (...) {
printf('<div id="product-header-%u" class="product-header">', $i++);
foreach (...) {
printf('<div id="product-contents-%u" class="product-contents">', $j++);
}
}

Categories