This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Multiple index variables in PHP foreach loop
I am trying to create a before and after image display using mysqli, php and html.
I am pulling the records from the db with mysqli and php, and am trying to view the results in an Unordered List using a foreach loop.
I have my 2 arrays which are created above in a while loop of a mysqli query. The array will only contain 5 results (for pagination, may change later).
$before_array[] = '
<div class="highslide-gallery">
<li>
<a href="'.$beforeurl.'" class="highslide" onclick="return hs.expand(this)">
<img src="'.$beforeurl.'" width="100px" height="125px" alt="Highslide JS">
</a>
</li>';
$after_array[] = '
<li><a href="'.$afterurl.'" class="highslide" onclick="return hs.expand(this)">
<img src="'.$afterurl.'" width="100px" height="125px" alt="Highslide JS">
</a>
</li>';
and in the view of the page I have the following code to display the before and after pictures.
<div id="hoverwrap">
<ul class="hoverbox">;
<?php
foreach ($before_array as $value) {
echo $value;
}
foreach ($after_array as $value) {
echo $value;
}
?>
</ul>
</div><!-- end of hoverwrap div-->
With the current code it is displaying the pictures :
before - before - before - before - before - after - after - after - after - after.
Where I would like it to diplay:
before - after - before - after etc etc.
I was thinking a while loop but no matter which way I try and implement it, it results in a similiar display.
Any help or pointers in the right direction will be greatly appreciated.
Thanks.
A for loop will do the trick here:
$x = count($before_array);
for($i = 0; $i < $x; $i++) {
print $before_array[$i];
print $after_array[$i];
}
Why can't you print it in the mysql loop itself?
Related
This question already has answers here:
The 3 different equals
(5 answers)
Closed 6 months ago.
I have a multidimensional array that is used to pull images and data into the page. The page will also have a sort by system which will display only the array items that have a certain tag (currently tagging yes or no with 0 and 1) I am using an if statement to decide whether the page displays the default (all of the photos) or the sorted array. The default works fine on its own, but im trying to get the sorted to display. When i include the html/php for displaying the images, instead of "echo $mImage['img_caption'];" (Which is a stand in, im just testing if i can even get the statement to display the items. Instead of the full caption string, it displays the first character of every value of every key.) i get errors akin to "unexpected "{" or "<" and the page wont load. Ive basically had to teach myself php over the course of three days, so im sure im missing something easy. Ill include an example of the array, the php for the sort, and the section that works to display the entire array.
Here is my arrays
$arrImages[] =
[
'img_sm'=>'exampleple.jpg',
'img_lg'=>'example2.thumb.jpg',
'img_caption'=>'multifamily',
'img_description'=>'example',
'img_path' => 'img/getest',
'img_type'=>'getest',
'MultiFamily'=>1,
];
$arrImages[] =
[
'img_sm'=>'example.jpg',
'img_lg'=>'example.thumb.jpg',
'img_caption'=>'notmultifamily',
'img_description'=>'example786',
'img_path' => 'img/getest',
'img_type'=>'getest',
'MultiFamily'=> 0,
];
And here is the if statement
<?php
$sorted = 1;
//If the viewer is sorting, check what they are sorting by and then sort by that before pushing the page to change display.
if ($sorted = 1){//start if sorted statement
$MultiFamilySorted = 1; //replace this with a function that checks all checkboxes to see if they are sorted by that or not. This is simulating that the viewer is sorting to see only multifamily homes.
foreach($arrImages as $sortedImages)://Start sort loop
if ($sortedImages['MultiFamily'] = 1){//start display sorted
foreach($sortedImages as $mImage): //start display loop
echo $mImage['img_caption'];//i am just using this to test if it will display
endforeach;
}//end display sorted
endforeach;
}//end if sorted statement
?>
This is the section for my default display
<div class="ImageDisplayTestBox">
<?php
foreach($arrImages as $mImage): //loop through the image array
?>
<div class="masonry-item no-default-style col-sm-3">
<a href="<?php echo $mImage['img_path'] . '/' . $mImage['img_lg']; ?>">
<span class="thumb-info thumb-info-centered-info thumb-info-no-borders">
<span class="thumb-info-wrapper">
<img src="<?php echo $mImage['img_path'] . '/' . $mImage['img_sm']; ?>" class="img-fluid" alt="">
<span class="thumb-info-title">
<span class="thumb-info-inner"><?php echo $mImage['img_caption']; ?></span>
<span class="thumb-info-type"><?php echo $mImage['img_type']; ?></span>
</span>
<span class="thumb-info-action">
<span class="thumb-info-action-icon"><i class="fas fa-plus"></i></span>
</span>
</span>
</span>
</a>
</div>
<?php endforeach; //end loop ?>
</div>
Thanks!
I did not check you code for the syntax error you mentioned, but there is a simpler way of doing this:
<?php
if ($sorted == 1){
$arrImages = array_filter($arrImages, function($image){
return $image['MultiFamily'] == 0;
});
}
?>
You can use this instead of your sorting code.
This executes the sorting function for each array member. If the function returns true it will pass it to the the sorted array. If it is false it will skip it.
The documentation of array_filter is here: https://www.php.net/manual/de/function.array-filter.php
I'll try to be clear. My problem is that i'm using php to get data from mysql. The table has more than one record. To show table records i'm using a while loop that as a condition has mysqli_fetch_array(). The records from the table must be shown in an echo(because i want to show them as html on the page), but every div generated from the while loop has a link to send me to another page, and if the client clicks on that link it will get the current divs information(after the table in database has more than one record the informations will be different) to be shown at the next page. Hope you understood it. Thanks in advance!
while($rows1 = mysqli_fetch_array($query1,MYSQLI_ASSOC))
{
echo "<div class=\"row\">
<div class=\"col-md-6\">
<div class=\"thumb\">
<figure>
<img src=\"images/extra-images/room-grid1.jpg\" alt=\"\"/>
<figcaption>
<a rel=\"prettyPhoto[gallery2]\" href=\"images/extra-images/room-grid1.jpg\">
<i class=\"fa fa-search\"></i>
</a>
</figcaption>
</figure>
</div>
</div>
<div class=\"col-md-6\">
<div class=\"text\">
<h4>".$rows1['Dh_lloji']."</h4>
<p>".$rows1['Dh_Pershkrimi']."</p>
<ul class=\"room-grid-meta\">
<li>Max: ".$rows1['Dh_Kapaciteti']."</li>
<li>Size: ".$rows1['Dh_madhesia']."</li>
<li>Floor: ".$rows1['Dh_Kati']."</li>
</ul>
<div class=\"retail room-grid-retail\">
<span>
<sup>$</sup>
".$rows1['Dh_cmimi']."
<sub>night</sub>
</span>";
if(isset($_SESSION['Emri_Mbiemri']) != '')
{
echo "<a class=\"btn-3\" href=\"payment.php\">Book now</a>";
}else{
$error = "You must be logged in to book!";
}
echo "<br>".$error."</div>
<div id=\"price_room\">".$rows1['Dh_cmimi']."</div>
</div>
</div>
</div>";
$count++;
}
Looks like you need at least your custom counter, that will be increment inside cycle, and set div's id based on this counter plus use it in generated link
I made a solution for my problem and i wanted to share it with you guys. I gave a $count to the links defining every link the while loop generates...
<a class=\"btn-3\" href=\"payment.php?id=$count\">Book now</a>
After that i used a multidimensional array to store the current divs variable(in my case rooms price, the count, and the id) of the room from database...
$room_prices[$count][0] = $rows1['Dh_cmimi'];
$room_prices[$count][1] = $count;
$room_prices[$count][2] = $rows1['ID_Dhoma'];
After the client click a specific link he will get the id so i can make a search at my multidimensional array for the price and the id of that room..
for($i = 0; $i < $count; $i++)
{
if($_GET['id'] == $room_prices[$i][1]){
$sess_room_price = $room_prices[$i][0];
$sess_room_id = $room_prices[$i][2];
}
}
After i get these i store them at the global session variables to use them at the other page...
$_SESSION['Rooms_Price'] = $sess_room_price;
$_SESSION['Rooms_ID'] = $sess_room_id;
So this did the work for me... Hope this helps others.
Sorry if the title is a bit confusing, I tried my best!
So basically I have 4 column divs that each contains 5 links for example. The links come from a table in my database, so new links are added and some others deleted, hence why I need to do it via database instead of writing it by hand. Now my issue is to divide the array in order to show 5 links per column (so when 5 links have been placed, div is closed and a new one is opened, unless there is no more link.
for example:
<div class="column">
Link
Link
Link
Link
Link
</div>
<div class="column">
Link
Link
Link
Link
Link
</div>
etc.
Thanks to anyone!
The PHP function array_chunk is nice to distribute an array of links into columns:
$columns = array_chunk($rows, 5);
foreach ($columns as $links)
{
echo '<div class="column">', "\n";
foreach ($links as $link)
printf("Link", $link);
echo '</div>', "\n";
}
I don't know your column names nor how you query the database, so I don't have any array indexes here written out. But I think you'll get the idea.
How about using the modulo operator
<div class="column">
for ($i = 0; $i < $nRow; $i++) {
if ($i % 5 == 0 && $i) {
echo '</div><div class="column">';
}
echo "<a href='{$links[$i]}'>Link</a>";
}
</div>
I was wondering if it's possible to have 2 'while' statements using the same 'mysql_query'?
I'm using a jquery thumb gallery with the results pulled in from a database. The gallery requires that the images are grouped together in an unordered list, with the text/overlayed content grouped together in hidden divs which need to be separate from the unordered list. The reason for doing this is because there are many potential results that could go in here and bringing them in randomly seems to make sense.
Here's the code I'm currently using: Any help greatly appreciated, S.
<div id="banner-wrap">
<div id="banner" class="gallery">
<ul class="galleryBar">
<?php
$homeB=mysql_query("select * from istable where fpGallery = '1' ORDER BY RAND() LIMIT 0, 5");
while($homeG=mysql_fetch_array($homeB)) {
$linkcode = $homeG['title'];
$linkcode = str_replace(" ","",$linkcode);
echo '
<li>
<a href="'.$wwwUrl.'images/'.$homeG['image'].'" rel="'.$linkcode.'">
<img src="'.$wwwUrl.'images/tn/'.$homeG['image'].'" width="75" height="55" alt="'.$homeG['title'].'" />
</a>
</li>
';
}
echo '</ul>';
echo '</div>';
while($homeGal=mysql_fetch_array($homeB)) {
echo '
<div id="'.$linkcode.'" class="overlay">
<h3>'.$homeGal['title'].'</h3>
<h4>'.$homeGal['location'].'</h4>
</div>
';
}
?>
</div>
Try to reset the iterator of the query result by calling mysql_data_seek($homeB, 0) between the two while loops.
WHERE `column1` = 'value1' AND `column2` = 'value2'
Your unindented code is hard to follow but it appears that you're asking whether mysql_fetch_array() starts again when it reaches the last row. The answer to that is no.
Can you rewind the result set? In theory, you can (find mysql_data_seek() in the manual). But there's no need to make it so complicate: just read data once, store it into an array and loop the array as many times as you need.
hey guys , i know this is a stupid question but i hanged in solving it
i wrote a block of php code to show images from mysql
echo "<table><tr> ";
while($cat = $db->sql_fetchrow($catinfo)) {
echo '
<td>
<ul id="three-col" class="press">
<li>
<div class="post-content">
<a class="zoom1" href="'.$galsettings[setImgDir].'/'.$cat[galCatLocation].'/'.$cat[galCatImg].'">
<img src="'.$galsettings[setImgDir].'/'.$cat[galCatLocation].'/'.$cat[galCatImg].'" alt="artistry (via powerbooktrance)" />
</a>
</div>';
for ($i=0; $i>2; $i++) {
echo "</tr><tr>";
}
}
echo "</li></ul></td></tr></table>";
but with this code everything goes wrong and it doesnt break after each 3 images in row
i even used
if ($i>2) {
echo "</td></tr><tr>";
}
but as u know it only breaks the tr after image number 3 not every row
im really sorry for my foolish question
Try this:
echo "<table><tr> ";
$counter = 0;
while($cat = $db->sql_fetchrow($catinfo)) {
$counter++;
echo '
<td>
<ul id="three-col" class="press">
<li>
<div class="post-content">
<a class="zoom1" href="'.$galsettings[setImgDir].'/'.$cat[galCatLocation].'/'.$cat[galCatImg].'">
<img src="'.$galsettings[setImgDir].'/'.$cat[galCatLocation].'/'.$cat[galCatImg].'" alt="artistry (via powerbooktrance)" />
</a>
</div>
';
if ( $counter == 3 ) {
echo '</tr><tr>';
$counter = 0;
}
}
echo "</li></ul></td></tr></table>";
First off, the
for ($i=0; $i>2; $i++) {
should probably be
for ($i=0; $i < 2; $i++) {
There may be additional logic issues (looking)...
Indeed the logic seems utterly flawed, for ex. there doesn't appear to be anything which detects the the third image etc...
==> I suggest you try the snippet from tambler's answer. No point in trying and fix this one... but if you must:
the for ($i=0; $i>2; $i++) { loop is unnecessary. Even when fixed to $i < 2 (or 3...) this doesn't do anything useful.
with each new image you output a <td>, this needs to be closed with a </td>
there need to be a counter (the $i) you hint at in your snippet isn't valid
the counter is to be systematically incremented with each image
a test is to be added towards the end of the loop:
If counter >= 3 (or 2, if you make it 0-based)
reset counter;
emit ""
Also the <ul> and <li> and their respective closing tags are misplaced.
Where's you're </td> tag?
All your TDs have to be inside the TRs. You have to close the UL and TD before your TR.