php - Displaying more than one image from a database - php

I am trying to display images (and other data connected to them, e.g. titles) in a table. The filenames of the images are stored in my database as imgname, but the problem is that there is only one image that is being displayed.
Here is the code I use. What should I change?
mysql_select_db($database_connection, $connection);
$query_img = "SELECT imgname FROM img ORDER BY imgname";
$img = mysql_query($query_img, $connection) or die(mysql_error());
$row_img = mysql_fetch_assoc($img);
$totalRows_img = mysql_num_rows($img);
Table
<ul>
<li>
<img src="images/<?php echo $row_img['imgname']; ?>">
<h3><?php echo $row_title['title']; ?></h3>
<p><?php echo $row_description['des']; ?></p>
</li>
<li>
<img src="images/<?php echo $row_img['imgname']; ?>">
<h3><?php echo $row_title['title']; ?></h3>
<p><?php echo $row_description['des']; ?></p>
</li>
<li>
<img src="images/<?php echo $row_img['imgname']; ?>">
<h3><?php echo $row_title['title']; ?></h3>
<p><?php echo $row_description['des']; ?></p>
</li>
</ul>
Since I use a simmilar code for the titles and descriptions, I have the same problem with them.

Just use a loop to iterate through the database resultset:
$img = mysql_query($query_img, $connection) or die(mysql_error());
while($row_img = mysql_fetch_assoc($img)) {
?>
<li>
<img src="images/<?php echo $row_img['imgname']; ?>">
<h3><?php echo $row_title['title']; ?></h3>
<p><?php echo $row_description['des']; ?></p>
</li>
<?php
}

mysql_select_db($database_connection, $connection);
$query_img = "SELECT imgname FROM img ORDER BY imgname";
$img = mysql_query($query_img, $connection) or die(mysql_error());
<ul>
<?php
while($row_img = mysql_fetch_assoc($img)) {
?>
<li>
<img src="images/<?php echo $row_img['imgname']; ?>">
<h3><?php echo $row_title['title']; ?></h3>
<p><?php echo $row_description['des']; ?></p>
</li>
<?php
}
?>
</ul>

Use a foreach to loop through the results and include the <li> in that loop to get a list element per row returned from the query.

$img = mysql_query($query_img, $connection) or die(mysql_error());
$result = mysql_fetch_assoc($img);
foreach ($result as $row_img)
{
?>
<li>
<img src="images/<?php echo $row_img['imgname']; ?>">
<h3><?php echo $row_img['title']; ?></h3>
<p><?php echo $row_img['des']; ?></p>
</li>
<?php
}

Related

Wordpress - strange symbol appeared beside the post thumbnail

Sorry I feel it an idiot question, but I can't fix it, those characters "> appeared beside the post thumbnail, and I don't know what's wrong there!
<?php
$notification_id = $notification->ID;
$notification_time = strtotime($notification->post_date);
$type = get_post_meta($notification_id, 'dw_notification_type', true);
$custom_type = get_post_meta($notification_id, 'dw_notification_custom_type', true);
$image = get_post_meta($notification_id, 'dw_notification_image', true);
?>
<li id="dw-notif-<?php echo $notification_id;?>" class="<?php echo dwnotif_check_user_read(false, $notification_id)?'read':'unread'; ?> <?php echo $type?$type:''; ?> <?php echo $custom_type?$custom_type:''; ?>" data-time="<?php echo $notification_time;?>">
<a href="<?php echo get_permalink($notification->ID);?>">
<div class="notif-avatar">
<?php if(has_post_thumbnail($notification->ID)): ?>
<?php echo get_the_post_thumbnail($notification->ID, 'post-thumbnail'); ?>
<?php elseif($image): ?>
<img src="<?php echo $image; ?>">
<?php else: ?>
<img src="<?php echo dwnotif_default_image(); ?>">
<?php endif; ?>
</div>
<div><?php echo $notification->post_title; ?></div>
<date class="notif-time"><?php echo human_time_diff( $notification_time, current_time('timestamp') ) . __( ' ago', 'dw-notifications' ); ?></date>
</a>
</li>

Converting to php MySQLi Object-Oriented

I want to convert my website to MySQLi object-oriented:
Below works. Before I convert my site is there anything I've missed or need to do better or differently?
Any help would be appreciated.
<?php
$query = "SELECT pagename, image, name FROM table";
$result = $db->query($query);
while ($row = $result->fetch_assoc())
{
?>
<figure>
<a href="<?php echo ($row['pagename']); ?>">
<img src="<?php echo ($row['image']); ?>"></a>
<figcaption>
<a href="<?php echo ($row['pagename']); ?>">
<h2><?php echo ($row['name']); ?></h2></a>
</figcaption>
</figure>
<?php
}
$result->free();
$db->close();
?>

each query a different background-image

I build this in php:
<?php
$bg = array('block2.png', 'block3.png', 'block4.png', 'block5.png', 'block7.png' );
$i = rand(0, count($bg)-1);
$selectedBg = "$bg[$i]";
?>
<style type="text/css">
<!--
.block_small{
background-image: url(/tableaux/customer/images/<?php echo $selectedBg; ?>);
}
-->
</style>
<?php
$db = new DB();
$db->query("SELECT * FROM vestigingen ");
while($item = $db->next_record()){
?>
<div class="block_small">
<img src="<?php echo $item['afbeelding']; ?>" />
<h1><?php echo $item['plaats']; ?></h1>
<div class="content_blocks">
<p><?php echo $item['adres']; ?></p>
<p><?php echo $item['postcode']; ?></p>
<p><?php echo $item['telefoon']; ?></p>
<p><?php echo $item['mail']; ?></p>
</div>
<div class="arrow"><?php echo $item['link']; ?></div>
</div>
<?
}
?>
Content loads from a SQL database, I want every block to have a different background-image, now they are all the same.
I first tried to put the style in the while loop but that didn't work.
Idea's ?
If you want all the div containers to have a different background you cannot append the same class to all of them. The image in the class is chosen randomly, but afterwards the same image-background is given to all the div elements with the same class.
With only php you got two possibilities to set a different background for all of the images:
first solution:
write a loop and set the css for ids #small_block1-#small_blockx.
second solution:
set inline css for all the elements within the style tag in a loop. I will show this solution below.
<?php
$bg = array('block2.png', 'block3.png', 'block4.png', 'block5.png', 'block7.png' );
$db = new DB();
$db->query("SELECT * FROM vestigingen ");
$counter = 0;
while($item = $db->next_record()){
?>
<div class="block_small" style="background-image: url(/tableaux/customer/images/<?php echo $bg[$counter]; ?>);" >
<img src="<?php echo $item['afbeelding']; ?>" />
<h1><?php echo $item['plaats']; ?></h1>
<div class="content_blocks">
<p><?php echo $item['adres']; ?></p>
<p><?php echo $item['postcode']; ?></p>
<p><?php echo $item['telefoon']; ?></p>
<p><?php echo $item['mail']; ?></p>
</div>
<div class="arrow"><?php echo $item['link']; ?></div>
</div>
<?
$counter++;
}
?>
You are do it in wrong way, you are always update the style. Instead of this, add an inline style.
<?php
$bg = array('block2.png', 'block3.png', 'block4.png', 'block5.png', 'block7.png');
$db = new DB();
$db->query("SELECT * FROM vestigingen ");
while ($item = $db->next_record()) {
$selectedBg = $bg[array_rand($bg)];
?>
<div class="block_small" style="background-image: url('/tableaux/customer/images/<?php echo $selectedBg; ?>)';">
<img src="<?php echo $item['afbeelding']; ?>" />
<h1><?php echo $item['plaats']; ?></h1>
<div class="content_blocks">
<p><?php echo $item['adres']; ?></p>
<p><?php echo $item['postcode']; ?></p>
<p><?php echo $item['telefoon']; ?></p>
<p><?php echo $item['mail']; ?></p>
</div>
<div class="arrow"><?php echo $item['link']; ?></div>
</div>
<?php
}

Simplified php code for displaying results

Sorry if this have been asked before but I couldn't find what I wanted and I am not strong in PHP.
Right now I have this code, which is supposed to return result for different levels:
<div class="swiper-slide">
<img src="img/B1.jpg" alt="" />
<div class="content_container">
<?php
$result = mysqli_query($con,"SELECT * FROM floor_directory WHERE level='B1'");
while($row = mysqli_fetch_array($result))
{
?>
<h1><?php echo $row['categories']; ?></h1>
<ul class="shop_listing clearfix">
<li class="float_left"><?php echo $row['name']; ?></li>
<li class="float_right"><?php echo $row['unit_number']; ?></li>
</ul>
<?php
}
?>
</div>
</div>
<div class="swiper-slide">
<img src="img/L1.jpg" alt="" />
<div class="content_container">
<?php
$result = mysqli_query($con,"SELECT * FROM floor_directory WHERE level='L1'");
while($row = mysqli_fetch_array($result))
{
?>
<h1><?php echo $row['categories']; ?></h1>
<ul class="shop_listing clearfix">
<li class="float_left"><?php echo $row['name']; ?></li>
<li class="float_right"><?php echo $row['unit_number']; ?></li>
</ul>
<?php
}
?>
</div>
</div> and so on...
Right now I can only duplicate it in order to fulfil the displaying of result for each individual levels. If let's say the building have 10 levels, is there a way to simplified the coding?
Hope you guys understand. Thanks in advance! =)
Try this:
<?php
$levelArray=array('L1','B1','L2','B2');
foreach ($levelArray as $i=>$level) {
$data='';
$img = "img/".$levelArray[$i];
$result = mysqli_query($con,"SELECT * FROM floor_directory WHERE level='$levelArray[$i]'");
while($row = mysqli_fetch_array($result)){
$data .= '<h1>'.$row['categories'].'</h1>
<ul class="shop_listing clearfix">
<li class="float_left">'.$row['name'].'</li>
<li class="float_right">'.$row['unit_number'].'</li>
</ul>';
}
echo '<div class="swiper-slide">
<img src="'.$img.'" alt="" />
<div class="content_container">'.$data.'</div>
</div>'
}
?>

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