I have a div of 40 images(nopreview.png), What I am trying to do is to replace the nopreview.png with the images from db, so if I have 10 images in DB, so out of 40 nopreview.png will be replaced with images from DB keeping the 30 nopreview.png as it is.
HTML
<div class="holder">
<img src="nopreview.png"/>
<img src="nopreview.png"/>
</div>
PHP
$uid="XXXXX";
$check = "SELECT rqid FROM users WHERE fbid = $uid LIMIT 0,250";
$rs = mysqli_query($con,$check);
if(mysqli_num_rows($rs)>0):
while($row = mysqli_fetch_assoc($rs)):
$reqid= $row['rqid'];
$requests = explode(',',$reqid);
foreach(array_unique($requests) as $request_id) {
echo $request_id."<br>";
echo"<img src='https://graph.facebook.com/$request_id/picture?width=78&height=78' />";
echo "<hr>";
}
endwhile;
endif;
Stuck in where to put the images div?
You're trying to work with PHP like Javascript.
Javascript is for DOM manipulation.
PHP is embedded into HTML, and creates HTML (most of the time).
Sccrap your HTML file. Your file "images.php" will look sth like:
<div class="holder">
<?php
$uid="XXXXX";
$check = "SELECT rqid FROM users WHERE fbid = $uid LIMIT 0,250";
$rs = mysqli_query($con,$check);
$imagecount=0;
while($row = mysqli_fetch_assoc($rs)) {
$reqid= $row['rqid'];
$requests = explode(',',$reqid);
foreach(array_unique($requests) as $request_id) {
echo"<img src='https://graph.facebook.com/$request_id/picture?width=78&height=78' />";
$imagecount++;
}
}
for(;$imagecount<40;$imagecount++) {
echo("<img src=\"nopreview.png\" />");
}
?>
</div>
So you will always have your 40 images, starting with the available ones and filling at the end with nopreview.png if required.
Related
I am trying to build a web page that has 6 rows of images. Each row has 4 images and each image has a caption right below it. So it goes something like this:
IMAGE IMAGE IMAGE IMAGE
TEXT TEXT TEXT TEXT
IMAGE IMAGE IMAGE IMAGE
TEXT TEXT TEXT TEXT
and so on.
This is my code:
<?php
$resultSet = $db->query("SELECT * FROM Articles");
if ($resultSet->num_rows != 0) {
while ($rows = $resultSet->fetch_assoc()) {
$image = $rows["image"];
$text = $rows["text"];
echo "<img class=images src=$image> <p class=texts>$text</p>";
}
}
?>
Both classes are set to display: inline-block. The code prints out each image next to the text. The text should be below the image. I am trying to think of ways of displaying the images and the texts in the proper format, but I can't seem to think of any solutions at the moment. Anyone can give me some insight?
Set only the wrapper class with the display: inline-block rule
<?php
$resultSet = $db->query("SELECT * FROM Articles");
if ($resultSet->num_rows != 0) {
while ($rows = $resultSet->fetch_assoc()) {
$image = $rows["image"];
$text = $rows["text"];
echo "<div class='wrapper'>";
echo "<img class=images src=$image> </br> <p class=texts>$text</p>";
echo "</div>";
}
}
?>
You should be able to achieve this using divs.
The following code will add in a row ever
<?php
$resultSet = $db->query("SELECT * FROM Articles");
if ($resultSet->num_rows != 0) {
$count = 0;
while ($rows = $resultSet->fetch_assoc()) {
$num_in_row = 4; // Number of items you want in each row
if($count % $num_in_row == 0){
echo '<div class="row">'; // if the row already has 4 items, add a new row.
}
$image = $rows["image"];
$text = $rows["text"];
echo "<span class='wrapper'>";
echo '<img class="images" src='.$image.'> <div class="texts">'.$text.'</p>';
echo "</span>";
if($count % $num_in_row == ($num_in_row-1)){
echo '</tr>';
}
$count++;
}
}
?>
That should give you a result that looks like the jsfiddle
I would like to display multiple images on my index page. Doesn’t matter if they are displayed in any order.
Each of the images have their image path and their unique ID. I have the following code but it is not displaying anything:
<?php
$result = mysql_query("SELECT * FROM my_image", $connection);
while($row = mysql_fetch_array($result))
{
$ID = $row['name'];
$file = $row['file_path'] ;
echo '<img style="margin: -32.5px 0 0 0;" alt="" src="'.$file.'">' ;
}
?>
Anyone know what could be wrong?
you should start to debug your code, as example :
while($row = mysql_fetch_array($result))
{
$file = $row['file_path'] ;
echo $file; continue;
//...
}
and see if you get expected result.
EDIT:
what about var_dump($result) ?
This question already has answers here:
Fatal error: [] operator not supported for strings
(9 answers)
Closed 7 years ago.
I wrote (thanks to Stackoverflow help, too) a PHP script (called wunder_temp.php) which shows the temperature and location for a given number of Weatherunderground.com Stations.
I included this script in my footer, and it works well BUT on a single page.
If I open http://www.flapane.com/guestbook/guestbook.php the temperatures won't be shown in my footer, and error.log says:
[09-Sep-2012 09:46:45 UTC] PHP Fatal error: [] operator not supported for strings in /home/xxx/public_html/wunder_temp.php on line 47
$display = file($cachename, FILE_IGNORE_NEW_LINES); //ignore \n for non-reporting stations
foreach ($display as $key=>$value){
if($key % 2 == 0){
$temperature[] = $value; // EVEN (righe del file cache pari)
}else{
$location[] = $value; // ODD - **HERE IS LINE 47**
}
}
The weird thing is that guestbook.php is the ONLY page of my website where wunder_temp.php doesn't work.
What the above code does is reading the cachefile and put in a $temperature[] array the even lines and in $location[] array the odd lines.
Here's a sample from my cachefile:
26.8
Stadio San Paolo di Napoli, Napoli
24.4
Pozzuoli
Honestly I don't know why I see that errors just on my guestbook page.
It turns out that the "culprit" is the function loadmore.php which loads the guestbook comments (and which is included in guestbook.php) using a twitter-style ajax function. If I don't include it, wunder_temp.php works well, and it doesn't produce any error.
loadmore.php:
<div id='contcomment'>
<div class="timeline" id="updates">
<?php
$sql=mysql_query("select * from gbook_comments ORDER BY id DESC LIMIT 9");
while($row=mysql_fetch_array($sql))
{
$msg_id=$row['id'];
$name=$row['name'];
$url=$row['url'];
$email=$row['email'];
$location=$row['location'];
$date= strtotime($row['dt']); //unix timestamp
$country_code=$row['country_code'];
$message=$row['body'];
$link_open = '';
$link_close = '';
if($url){
// If the person has entered a URL when adding a comment,
// define opening and closing hyperlink tags
$link_open = '<a href="'.$url.'" target="_blank" rel="nofollow">';
$link_close = '</a>';
}
// Needed for the default gravatar image:
$url_grav = 'http://'.dirname($_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"]).'/img/default_avatar.gif';
// Show flags
$image = strtolower($country_code) . ".png";
if (file_exists("./img/flags/" . $image)){
$show_image = true;
$image_link = "<img src='/guestbook/img/flags/$image' alt='user flag' />";
}else{
$show_image = false;
}
echo '<div class="comment">
<div class="avatar">
'.$link_open.'
<img src="http://www.gravatar.com/avatar/'.md5($email).'?size=50&default='.urlencode($url_grav).'" alt="gravatar icon" />
'.$link_close.'
</div>
<div class="name">'.$link_open.$name.$link_close.' </div><div class="location"><i>(da/from '.$location.' '.$image_link.' )</i></div>
<div class="date" title="Added at '.date('H:i \o\n d M Y',$date).'">'.date('d M Y',$date).'</div>
<p>'.$message.'</p>
</div>' ;
} ?>
</div>
<div id="more<?php echo $msg_id; ?>" class="morebox">
Carica Commenti più vecchi / Load older entries
</div>
</div>
ajax_more.js AJAX twitter-style load-more-comments function:
$(function() {
//More Button
$('.more').live("click",function()
{
var ID = $(this).attr("id");
if(ID)
{
$("#more"+ID).html('<img src="moreajax.gif" />');
$.ajax({
type: "POST",
url: "ajax_more.php",
data: "lastmsg="+ ID,
cache: false,
success: function(html){
$("div#updates").append(html);
$("#more"+ID).remove();
}
});
}
else
{
$(".morebox").html('Nessun altro commento / No more comments');
}
return false;
});
});
ajax_more.php (needed by the above script):
<?
include "connect.php";
if(isSet($_POST['lastmsg']))
{
$lastmsg=$_POST['lastmsg'];
$lastmsg=mysql_real_escape_string($lastmsg);
$result=mysql_query("select * from gbook_comments where id<'$lastmsg' order by id desc limit 9");
$count=mysql_num_rows($result);
while($row=mysql_fetch_array($result))
{
$msg_id=$row['id'];
$name=$row['name'];
$url=$row['url'];
$email=$row['email'];
$location=$row['location'];
$date= strtotime($row['dt']); //unix timestamp
$country_code=$row['country_code'];
$message=$row['body'];
$link_open = '';
$link_close = '';
if($url){
// If the person has entered a URL when adding a comment,
// define opening and closing hyperlink tags
$link_open = '<a href="'.$url.'" target="_blank" rel="nofollow">';
$link_close = '</a>';
}
// Needed for the default gravatar image:
$url_grav = 'http://'.dirname($_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"]).'/img/default_avatar.gif';
// Show flags
$image = strtolower($country_code) . ".png";
if (file_exists("./img/flags/" . $image)){
$show_image = true;
$image_link = "<img src='/guestbook/img/flags/$image' alt='user flag' />";
}else{
$show_image = false;
}
echo '<div class="comment">
<div class="avatar">
'.$link_open.'
<img src="http://www.gravatar.com/avatar/'.md5($email).'?size=50&default='.urlencode($url_grav).'" alt="gravatar icon" />
'.$link_close.'
</div>
<div class="name">'.$link_open.$name.$link_close.' </div><div class="location"><i>(da/from '.$location.' '.$image_link.' )</i></div>
<div class="date" title="Added at '.date('H:i \o\n d M Y',$date).'">'.date('d M Y',$date).'</div>
<p>'.$message.'</p>
</div>' ;
}
?>
<div id="more<?php echo $msg_id; ?>" class="morebox">
Carica Commenti più vecchi / Load older entries
</div>
<?php
}
?>
Any help is appreciated
$location is already a string on that page. This is why you properly initialize variables before you use them:
$temperature = $location = array();
foreach ($display as $key => $value){
if ($key % 2 == 0){
$temperature[] = $value;
} else {
$location[] = $value;
}
}
Even better, separate your variable scopes better so you don't get a name clash like that. Use functions and classes with their private variable scopes and don't put everything in the global scope.
It seems a matter of variable scope.
When you include loadmore.php in guestbook.php you are implicitly declaring $location as a string:
$location=$row['location'];
So this line of your loop:
$location[] = $value; // ODD - **HERE IS LINE 47**
is not implicitly declaring a new array variable on the first iteration but trying to append $value to the previously declared string variable, hence the error.
Try giving a different name to $location either in loadmore.php or guestbook.php or making loadmore.php a function (so it runs in its own scope) and then call it from your guestbook.php script after including its code on it. Or use explicit declaration of $location variable if you want to reuse it as an array (just add $location = array(); before the loop).
I think that loadmore.php and/or ajaxmore.php are setting a global variable ($location) to a string. When the guestbook page tries to index this variable you get the error.
Solution: use functions and local variables.
$location=$row['location']; is the issue. Because PHP doesn't have block level scope, you are essentially pre-setting this variable and trying to set it's array index using []. Because it's a string, that will break.
You should always initialize your variables to avoid these kinds of conflicts:
i.e.
$location = array();
Rename $location for one of the scripts. You have $location=$row['location'] somewhere. Also, declare the variable when using it as an array:
$array = array();
$array[] = 'item'; // when you add an item here, $array will always accept an array push.
I'm not good with PHP, so please bear with me. I have the following code:
<?php $thisPage="designers";
include("phpincludes/header.php") ?>
<div id="contentLeft">
<?echo "<h2><a href='designer_display.php?d_name=".$_GET['d_name']."'>" . $_GET['d_name']. "</a></h2>";?>
<?
error_reporting(0);
require_once "phpincludes/connection.php";
$designer = $_GET['d_name'];
// Category Selection Start.
$cat_qry = "SELECT DISTINCT (
`own_category`
)
FROM `products`
WHERE `designer` ='".$designer."' && own_category != ''";
$rs_qry = mysql_query($cat_qry);
$i = 0;
while($rec_qry = mysql_fetch_array($rs_qry))
{
if($i==0)
$first_cat = $rec_qry['cat_name'];
$cat_name[$i]=$rec_qry['cat_name'];
$i++;
}
// Category Selection Start.
$cat_name = $_GET['catName1'];
$cat_qry = "SELECT DISTINCT (
`own_category`
)
FROM `products`
WHERE `designer` ='".$designer."' && own_category != ''";
//"select * from categories";
$rs_qry = mysql_query($cat_qry);
$rec_no = mysql_affected_rows();
/*if($_GET["catName1"]=="")
$catName = $first_cat;
else
$catName = $cat_name;*/
$n1 = 1;
echo "<ul id=\"designers\">";
while($rec_qry = mysql_fetch_array($rs_qry))
{
$cate_name = str_replace('_',' ',$rec_qry['own_category']);
//print_r($cate_name[1]);
if($rec_qry["own_category"]!= $_GET['catName'])
echo "<li><A HREF='d_items.php?no=".$n1."&d_name=".$designer."&catName=".$rec_qry["own_category"]."'>".strtoupper($cate_name)."</A></li>";
else
echo "<li><A HREF='d_items.php?no=".$n1."&d_name=".$designer."&catName=".$rec_qry["own_category"]."'><font color='#8d9354'>".strtoupper($cate_name)."</font></a></li>";
if($rec_qry["own_category"]== $_GET['catName'])
{
$query="SELECT A.photo_filename, A.photo_title, B.dc_cat_name FROM displays A
LEFT JOIN displays_categories B ON B.dc_display_photos = A.photo_filename
WHERE dc_cat_name ='".$rec_qry["cat_name"]."'";
$query="SELECT B.pro_name, B.own_category, B.own_photo_filename from categories as A LEFT JOIN
products as B ON A.cat_name = B.own_category
where cat_name ='".$_GET["catName"]."' and designer ='".$designer."' order by B.pro_name";
$rs1_qry = mysql_query($query);
echo "<ul class=\"items\">";
while($row = mysql_fetch_array($rs1_qry))
{
if ($designer == "Jardan") {
$p1name = str_ireplace($designer,'',$row["pro_name"]);
$pname = substr($p1name, 0, -3);
} else {
$pname = str_ireplace($designer,'',$row["pro_name"]);
}
if($_GET['ProName'] != $row["pro_name"])
echo "<li><A HREF='d_item_details.php?d_name=".$designer."&ProName=".$row['pro_name']."&catName1=".$rec_qry['own_category']."&catName=".$rec_qry['own_category']."'>".$pname."</A></li>";
else
echo "<li><A HREF='d_item_details.php?d_name=".$designer."&ProName=".$row['pro_name']."&catName1=".$rec_qry['own_category']."&catName=".$rec_qry['own_category']."'><font color='#fff'>".$pname."</font></A></li>";
}
echo "</ul>";
}
}
echo "</ul>";
$f=1;
$recnm = $_GET['ProName'];
$owncat = $_GET['catName1'];
$photo_title = $_GET['ptitle'];
$query2="SELECT pro_code, pro_dimensions, own_photo_filename, designer_pdf, palette FROM products
WHERE pro_name ='".$recnm."' and own_category ='".$owncat."'";
$rt2=mysql_query($query2);
echo mysql_error();
?>
</div>
<div id="contentRight">
<?
while($row2 = mysql_fetch_array($rt2))
{
?>
<?$d = explode(' ',$designer);
for($p=0;$p<count($d);$p++)
{
$des.=$d[$p];
}
if ($designer == "Playstar") {
$p2name = str_ireplace($designer,'',$recnm);
$poname = substr($p2name, 0, -3);
} else {
$poname = str_ireplace($designer,'',$recnm);
}
?>
<img class="lighting" src="img/designers/<?echo $des."/".$row2['own_photo_filename'];?>.jpg" />
<div class="mailerBtn"><h4>ENQUIRE</h4>
<h4>Download Product PDF</h4></div>
<h3><?echo $poname;?></h3>
<p>
<?
echo "<b>Product code:</b> ". $row2['pro_code']."<BR>";
if ($designer == "Playstar") {
echo $row2['pro_dimensions'];
} else {
echo "<b>Dimensions:</b> ". $row2['pro_dimensions'];
} ?>
</p>
<? if($row2[4]=='yes') {
?>
<img class="palette" src="img/designers/<?echo $des."/".$row2['own_photo_filename'];?>-palette.jpg" />
<? } ?>
<?}?>
<?php include("phpincludes/base.php") ?>
Much of the code was written by someone else, but I've been modifying it in a couple of ways. It works locally (on XAMP) and on my personal site where I've been hosting it as a test site.
But when i uploaded it to the client's host, this page stops abruptly at echo "<ul class=\"items\">"; - roughly line 73. I can't see what's stopping it from running properly.
Some assistance would be gratefully received!
MTIA.
That's hard to tell. It's very obviously something with the clients setup.
Taking a wild guess, that client is still running PHP4. Because after line 73 you have a call to str_ireplace which wasn't available for that.
You would likely get a fatal error for this one. And this is the right avenue for investigation here. Add this on top for debugging (instead of error_reporting(0) which is not so helpful):
error_reporting(E_ALL|E_WARNING);
And ask for errors. Better yet, provide a custom error handler which prints out something shiny for end user-type clients. Otherwise ask for the error.log which should contain the exact error likewise.
You should avoid using the "short tags" <? and replace with the regular <?php tags. At a minimum, on that line not having a space after the "?" is asking for trouble, but overall you should just replace the short tags as they can cause trouble for various reasons and many installations do not have them enabled by default.
FYI, one specific case where they often cause trouble is for XHTML documents, if the xml declaration isn't printed with PHP, it will throw an error. Now with HTML5 I guess this will be less of an issue, but IMHO, best practice would be to avoid them.
Glad you got it working, but I wouldn't be using this code in production on your clients web host.
$f=1;
$recnm = $_GET['ProName'];
$owncat = $_GET['catName1'];
$photo_title = $_GET['ptitle'];
$query2="SELECT pro_code, pro_dimensions, own_photo_filename, designer_pdf, palette FROM products WHERE pro_name ='".$recnm."' and own_category ='".$owncat."'";
This and all the other queries here are vulnerably to sql injection. (if I passed in catName1='; DELETE * FROM products where 1=1 or '2'='2)
You need to either convert the queries to paramaterised queries, or use mysql_real_escape_string.
ie
$owncat = mysql_real_escape_string($_GET['ProName']);
I am trying to .load a script called 'refreshImages.php'. Inside that script is a while loop pulling from the database. I have got it to load a single echo function but it wont load anything inside the while loop I have on the script... this is what the php file has...
<?php
include 'includes/config.php';
$pimages = mysql_query("SELECT * FROM property_images WHERE pid='$pid'");
//Cant Post Images So Leaving The Echo Content Out//
while($img = mysql_fetch_array($pimages)){
$image = $img['image'];
$image_alt = $img['image_alt'];
echo "<li>$img</li>";
}?>
I am using .load('refreshImages.php') on the page I need it to show up on.
Any explanation I am not seeing?
Your $img is an array, not a string. You will get output like <li>Array</li>, if you have stuff coming from the database. Is that what you mean? Or are you getting an empty result?
If empty - what does your mysql_num_rows tell you when ran against the result resource?
try changing this:
echo "<li>$img</li>";
to
echo "<li><img src=\"{$image}\" alt=\"{$image_alt}\" /></li>";
You may not be getting any results from the database. Try using this code which will display a message if there is something wrong with your sql query.
<?php
include 'includes/config.php';
$pimages = mysql_query("SELECT * FROM property_images WHERE pid=" . $pid );
if (mysql_num_rows($pimages) > 0) { // checks to see if you are getting results from db
while($img = mysql_fetch_array($pimages)){
$image = $img['image'];
$image_alt = $img['image_alt'];
echo '<li><a class="thumb" href="{$image}"><img src="{$image}" width="50px" height="50px" alt="{$image_alt}"></a></li>';
}
} else {
echo "no results returned from database";
} // end of mysql_num_rows check
?>
You might be better off concatenating all the images and then echo-ing it out rather than echo-ing each one e.g
$htmlOutput = '';
while($img = mysql_fetch_array($pimages)){
$image = $img['image'];
$image_alt = $img['image_alt'];
$htmlOutput .= "<li><img src=\"{$image}\" alt=\"{$image_alt}\" /></li>";
}
echo $htmlOutput ;