Get two adjacent images in a while loop PHP - php

I have a html code , when i convert into php some issues happended . Actually i need a foreach or while loop for the below div.
But all images are different (never mind the names of images, it can be any name).
<div class='img'>
<img src='img/1.jpg'>
<img src='img/2.jpg'>
</div>
<div class='img'>
<img src='img/3.jpg'>
<img src='img/4.jpg'>
</div>
//....more divs like this
Here my try
$stmt=$db->query("Select * from photo");
foreach ($stmt as $row)
{
?>
<div class='img'>
<img src='img/<?php echo $row['image']; ?>'>
<img src='img/<?php echo $row['image']; ?>'> // how to get this image diffent from above images
</div>
<?php }
Any experts?

Try that:
<?php
$pdo = new PDO('mysql:host=localhost;dbname=test', 'root', '');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sth = $pdo->prepare("SELECT * FROM photo");
$sth->execute();
$data = $sth->fetchAll(PDO::FETCH_ASSOC);
?>
<?php $i = 0; ?>
<?php $response = array(); ?>
<?php foreach ($data as $dataIndex => $dataValue): ?>
<?php if ($i == 1): ?>
<?php $response[] = '<img src="img' . $dataValue["image"] . '"/>'; ?>
<?php $response[] = "</div>"; ?>
<?php $i = 0; ?>
<?php else: ?>
<?php $response[] = "<div class='img'>"; ?>
<?php $response[] = '<img src="img' . $dataValue["image"] . '"/>'; ?>
<?php $i++; ?>
<?php endif; ?>
<?php endforeach; ?>
<?php if(count($data)%2): ?>
<?php $response[] = "</div>"; ?>
<?php endif; ?>
<?php echo implode("", $response); ?>
output:
<div class="img">
<img src="img1">
<img src="img2">
</div>
<div class="img">
<img src="img3">
<img src="img4">
</div>
<div class="img">
<img src="img5">
<img src="img6">
</div>

Assuming $stmt is an array, you can do this.
<?php
$count = 0
while(sizeof($stmt) > $count) {
?>
<img src="<?php $stmt[$count]['image']?>"/>
<?php
$count++;
?>
<img src="<?php $stmt[$count]['image']?>"/>
<?php $count++; ?>
}
?>

Related

Generate HTML code with PHP automatically, after data pull in MySql

I have written a really simple php page that populate a database.
I now want to fetch this data in another php page and that is ok.
What I would like to achive is:
when I add another row into the database, I would like the html to create a new card, and not add the information in the same card.
I am not sure I understand how this can be achived.
Do I have to use php templates like smarty or anybody can point me how could I proceed?
This is how it look when I add second raw:
While what i want to achive should look like
Here is the HTML code I use with the PHP code:
<section class="tm-section-3 tm-section-mb" id="tm-section-3">
<div class="row">
<div class="col-md-6 tm-mb-sm-4 tm-2col-l">
<div class="image">
<img src="img/tm-img-1.jpg" class="img-fluid" />
</div>
<div class="tm-box-3">
<h2>
<?php if (mysqli_num_rows($result) > 0) {
?>
<table>
<?php
include_once '../scripts/connection.php';
$result = mysqli_query($link,"SELECT
domain,subdomain,topic,topictitle,topictext FROM newpage");
$i=0;
while($row = mysqli_fetch_array($result)) {
?>
<tr>
<td><?php echo $row["domain"]; ?></td>
</tr>
<tr>
<td><?php echo $row["subdomain"]; ?></td>
</tr>
<tr>
<td><?php echo $row["topic"]; ?></td>
</tr>
<tr>
<td><h4><?php echo $row["topictitle"]; ?></h4></td>
</tr>
<tr>
<td><h5><?php echo $row["topictext"]; ?></h5></td>
</tr>
<?php
$i++;
}
?>
</table>
<?php
}
else{
echo "No result found";
}
?>
</h2>
<p>
</p>
<div class="text-center">
Details
</div>
</div>
</div>
</div>
</section>
This is how i send the code to the db:
<?php
include("connection.php");
$domain = mysqli_real_escape_string($link, $_POST['domain']);
$subdomain = mysqli_real_escape_string($link, $_POST['subdomain']);
$topic = mysqli_real_escape_string($link, $_POST['topic']);
$topictitle = mysqli_real_escape_string($link, $_POST['topictitle']);
$topictext = mysqli_real_escape_string($link, $_POST['topictext']);
$sql = "INSERT INTO newpage (domain,subdomain,topic,topictitle,topictext) VALUES ('$domain','$subdomain','$topic','$topictitle','$topictext')";
$result = mysqli_query($link, $sql);
// if query fails stop script and echo error
if( $result === false)
{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
exit;
}
$sql = "INSERT INTO menu (item) VALUES ('$domain')";
$result = mysqli_query($link, $sql);
// if query fails stop script and echo error
if( $result === false)
{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
exit;
}
header("location:../scripts/add-new-page-script-end.php");
exit;
echo "You'll never see this";
?>
Here the code that works even the style is bad. But logically is correct:
<div class="col-md-6 tm-mb-sm-4 tm-2col-l">
<?php
include_once '../scripts/connection.php'; $result = mysqli_query($link,"SELECT domain,subdomain,topic,topictitle,topictext FROM newpage"); foreach($result as $row){
?>
<div class="image">
<img src="img/tm-img-1.jpg" class="img-fluid" />
</div>
<div class="tm-box-3">
<h1><?php echo $row['domain']; ?></h1>
<h2><?php echo $row['subdomain']; ?></h2>
<h3><span><?php echo $row['topic']; ?></span></h3>
<h4> <span><?php echo $row['topictitle']; ?></span></h4>
<p><?php echo $row['topictext']; ?></p>
<div class="text-center">
Details
</div>
</div>
<?php
}
?>
</div>
It currently looks like you have something like this:
<div class="card">
<img src="..." />
<?php
foreach($result as $row){
?>
<h1><?php echo $row['domain-name']; ?></h1>
<h2><?php echo $row['sub-domain-name']; ?></h2>
<span><?php echo $row['topic-text-title']; ?></span>
<p><?php echo $row['text-of-topic']; ?></p>
<?php
}
?>
<button>Details</button>
</div>
If you instead put the foreach loop outside of the card div then it will make a new card for each result, something like this:
<?php
foreach($result as $row){
?>
<div class="card">
<img src="..." />
<h1><?php echo $row['domain-name']; ?></h1>
<h2><?php echo $row['sub-domain-name']; ?></h2>
<span><?php echo $row['topic-text-title']; ?></span>
<p><?php echo $row['text-of-topic']; ?></p>
<button>Details</button>
</div>
<?php
}
?>
Assuming that your are not using any framework.
In raw php context you could do something like this:
<div>
<?php foreach($arr as $item): ?>
<div>
<img src="...">
<h1><?php echo $item->domain_name; ?></h1>
<h2><?php echo $item->subdomain_name; ?></h2>
<h3><?php echo $item->topic; ?></h3>
<h4><?php echo $item->topic_text_title; ?></h4>
<h5><?php echo $item->text_pf_topic; ?></h5>
</div>
<?php endforeach; ?>
</div>

Trouble with html link tag and image

Got some trouble when i tried to use an url to image.
<div class="col-lg-12">
<h1 class="page-header">Anime!</h1>
</div>
<?php
include "config/database.php";
$sql = "SELECT * FROM anime WHERE status = 'On Going' ORDER BY id";
$query = mysql_query($sql);
if ($query > 0){
?>
<div class="container">
<div class="description-plate">
<?php
while
($row = mysql_fetch_array($query)){
$id = $row['id'];
$image = $row['image'];
$title = $row['title'];
$genre = $row['genre'];
$start = $row['start'];
$schedule = $row['schedule'];
$description = $row['description'];
?>
<!--div class="caption-btm">
<p style="margin-left:6px; margin-top:175px;">Start Airing From:</p>
<h5 style="margin-left:10px;"><?php echo $start; ?></h5>
<p style="margin-left:6px;">Airing Schedule:</p>
<h5 style="margin-left:10px;"><?php echo $schedule; ?></h5>
</div-->
<div class="thumbnail-fluid">
<a href="<?php echo $row['image']; ?>">
<div id="og-plate">
<div><img src="admin/<?php echo $row['image']; ?>"></div>
<?php } ?>
</div>
</a>
</div>
</div>
<?php } ?>
</div>
So when i tried to call the image using php, the tag only appear on the last image. What i'm trying to do is having the tag on every images. Would appreciate any help, thanks :)
Right now you are going through the loop (not sure why you are using while) and each time creating
<div class="thumbnail-fluid">
<a href="<?php echo $row['image']; ?>">
<div id="og-plate">
<div><img src="admin/<?php echo $row['image']; ?>"></div>
<?php } ?>
</div>
</a>
</div>
What you want to do is build up an html string on each pass appending the next image tag, something more like
...
$myimages = '';
while // skipped details
$myimages .= ' <div class="thumbnail-fluid">
<a href=". $row['image'] . '>
<div id="og-plate">
<div><img src="admin/' . $row['image'] . '></div>'
. '</div>
</a>
</div>';
}
Its appear last image because ORDER BY id and the condition status = 'On Going' can return one image. Your html structure should be like this.
<div class="col-lg-12">
<h1 class="page-header">Anime!</h1>
</div>
<?php
include "config/database.php";
$sql = "SELECT * FROM anime WHERE status = 'On Going' ORDER BY id";
$result = mysql_query($sql);
$n = mysql_num_rows($result);
if ($n > 0) {
?>
<div class="container">
<div class="description-plate">
<?php
while ($row = mysql_fetch_array($result)) {
$id = $row['id'];
$image = $row['image'];
$title = $row['title'];
$genre = $row['genre'];
$start = $row['start'];
$schedule = $row['schedule'];
$description = $row['description'];
?>
<div class="thumbnail-fluid">
<a href="<?php echo $row['image']; ?>">
<div id="og-plate">
<div><img src="admin/<?php echo $row['image']; ?>"></div>
</div>
</a>
</div>
<?php } ?>
</div>
</div>
<?php } ?>

Show articles in view

I have a question:
I make a pre for news:
Array
(
[0] => Array
(
[name] => tag
[id] => 57
[title] => Article1,Article2,Article3
[views] => 53,54,58
[smallimage] => Koala-08.jpg,Jellyfish-08.jpg,Mountain-08.jpg
[date] => 2014-05-07 09:21:58,2014-05-08 09:24:38,2014-05-08 14:36:40
)
)
How to create the foreach in view to show 1 title 1 views and 1 date...I create an foreach but show first all titles,all views,all dates;
My foreach:
<?php if ($news):?>
<?php foreach($news as $n):?>
<p>Tag:<?php echo $n['name'] ?></p>
<div class="container">
<img src="<?php echo config_item('content_folder');?>news/small/<?php echo $n['smallimage']; ?>" alt="">
<?php echo $n['title'] ?><br/>
<?php echo $n['views'] ?>
<?php endforeach ?>
<?php endif; ?>
With this foreach I get: Article1,Article2,Article3 53,54,58...I want to get Article1 53,Article2 54, Article3 58....Help me please
Try this:
<?php
$news = array(array( "tag",
57,
"Article1,Article2,Article3",
"53,54,58",
"Koala-08.jpg,Jellyfish-08.jpg,Mountain-08.jpg",
"2014-05-07 09:21:58,2014-05-08 09:24:38,2014-05-08 14:36:40"
) );
?>
<?php if ($news){?>
<p>Tag:<?php echo $news[0][0]; ?></p>
<div class="container">
<img src="<?php echo config_item('content_folder');?>news/small/<?php echo $news[0][4]; ?>" alt="">
<?php $views = explode(',',$news[0][3]);?>
<?php $article = explode(',',$news[0][2]);?>
<?php for($i=0;$i<count($article);$i++){
echo $article[$i].'-'.$views[$i];
} ?>
</div>
<?php }?>
I don't know where is "config_item('content_folder')" function result.Try this. And let me know whether it is works or not. Feel free to ask help.
<?php foreach($news as $n):?>
<?php $titles = explode(",", $n['title']); ?>
<?php $smallimages = explode(",", $n['smallimage']); ?>
<?php $views= explode(",", $n['views']); ?>
<p>Tag:<?php echo $n['name'] ?></p>
<div class="container">
<img src="<?php echo config_item('content_folder');?>news/small/<?php echo smallimages[0]; ?>" alt="">
<?php echo title[0] ?><br/>
<?php echo views[0] ?>
<img src="<?php echo config_item('content_folder');?>news/small/<?php echo smallimages[1]; ?>" alt="">
<?php echo title[1] ?><br/>
<?php echo views[1] ?>
<img src="<?php echo config_item('content_folder');?>news/small/<?php echo smallimages[2]; ?>" alt="">
<?php echo title[2] ?><br/>
<?php echo views[2] ?>
<?php endforeach ?>
<?php endif; ?>
I am assuming that for example 'Article1,Article2,Article3' is a string, so you would have to use:
$n['title'] = explode(',',$n['title'])
To make it into an array. Same goes for views, smallimage and date. Here's an example on how to apply that:
<p>Tag:<?php echo $n['name'] ?></p>
<div id="container">
<?php
$n['title'] = explode(',',$n['title'])
$n['views'] = explode(',',$n['views'])
$n['smallimage'] = explode(',',$n['smallimage'])
foreach ($n['title'] as $key=>$value) {
echo "<img src='" . config_item('content_folder') . "news/small" . $n['smallimage'][$key] . "' alt='' />";
echo $n['title'][$key] . "<br />";
echo $n['views'][$key]
}
?>
</div>
You would place this in the existing foreach-loop.

Dont echo if value is a duplicate

I am using the code below to loop through all simple products of a configurable within Magento. The code shows specific colour data from each simple product.
However if there is two simple products in different sizes but both have the same colour it will echo the information about that colour twice I only need it to show it once.
<div class="colour-swatch">
<h1>Other Colours Available</h1>
<?php
$conf = Mage::getModel('catalog/product_type_configurable')->setProduct($_product);
$col = $conf->getUsedProductCollection()->addAttributeToSelect('*')->addFilterByRequiredOptions(); ?>
<div class="relative">
<?php
foreach($col as $simple_product){ ?>
<div class="container-swatch">
<img width="35" height="35" src="<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . 'catalog/product' . $simple_product->getSwatch() ?>">
<div class="content">
<div class="inside-swatch-name"><?php echo $simple_product->getAttributeText('real_colour'); ?></div>
<img src="<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . 'catalog/product' . $simple_product->getLargeSwatch() ?>">
</div>
</div>
<?php } ?>
<?php if ($synb == 'Yes') { ?>
<div class="swatch-order">
ORDER SAMPLES
</div>
<?php } else {
//do nothing
} ?>
</div>
</div>
$colors = array();
foreach($col as $simple_product){
$color = $simple_product->getAttributeText('real_colour');
if(!in_array($color, $colors)){
$colors[] = $color; ?>
//do the rest from your foreach
Change this:
$col = $conf->getUsedProductCollection()->addAttributeToSelect('*')->addFilterByRequiredOptions(); ?>
to this:
$col = $conf->getUsedProductCollection()->addAttributeToSelect('*')->addFilterByRequiredOptions()->addGroupByAttribute('real_colour'); ?>
and see if this works.

Create a dynamic XML href in PHP with SimpleXML

I'm trying to create a link based on a URL variable using only the last five digits of its respective line of XML data.
For example, if the XML link is http://events.stanford.edu/events/213/21389 how can I create this link a href="e/?i=21389?
Here's my page, XML and code:
<?php
// Build the XML file path, using URL variable $c (above)
$c = $_GET['c'];
$p ="http://events-prod.stanford.edu/xml/byCategory/";
$e = "/mobile.xml";
$file = "$p$c$e";
$xml = simplexml_load_file($file);
?>
<h1><?php echo $xml->title; ?></h1>
Home
</div><!-- /header -->
<div data-role="content">
<?php // Only display if there are events ?>
<?php if (isset($xml->Event->title)) { ?>
<ul data-role="listview">
<?php foreach($xml->Event as $event) { ?>
<li>
<a href="<?php echo $event->link; ?>">
<?php if ($event->Media->url != null) { ?>
<img src="<?php echo $event->Media->url;?>" alt="<?php echo $event->title;?> thumbnail" />
<?php } ?>
<h3><?php echo $event->title; ?></h3>
<p><strong><?php echo $event->beginDate; ?> at <?php echo $event->beginTime; ?></strong></p>
<p><?php echo $event->locationText; ?></p>
</a>
</li>
<?php } ?>
</ul>
<?php } else { ?>
<?php echo '<p>There is currently nothing scheduled for ', $xml->title, '.</p>';?>
<?php } ?>
I'm using short tags, because I think you'll agree it's easier now to read the code as oppose to before.
<?
$controller ="http://events-prod.stanford.edu/xml/byCategory/";
$category_id = $_GET['c'];
$category_id = 0;
$xml = "/mobile.xml";
$url = $controller . $category_id . $xml;
$xml_object = simplexml_load_file($url);
?>
<div>
<h1><?= $xml_object->title ?></h1>
Home
</div>
<div data-role="content">
<? if (!empty($xml_object->Event->title)): ?>
<ul data-role="listview">
<? foreach($xml_object->Event as $event): ?>
<li>
<?
$pattern = '/[0-9]+$/';
$matches = array();
preg_match($pattern, $event->link, $matches);
$my_link = 'e/?i=' . $matches[0];
?>
<a href="<?= $my_link ?>">
<? if (!empty($event->Media->url)): ?>
<img src="<?= $event->Media->url ?>" alt="<?= $event->title ?> thumbnail" />
<? endif; ?>
<h3><?= $event->title ?></h3>
<p><strong><?= $event->beginDate ?> at <?= $event->beginTime ?></strong></p>
<p><?= $event->locationText ?></p>
</a>
</li>
<? endforeach; ?>
</ul>
<? else: ?>
<? echo '<p>There is currently nothing scheduled for ', $xml_object->title, '.</p>'; ?>
<? endif; ?>
</div>

Categories