the below while loop statement loops mysqli query result(text and image from database). A row can contain text only or image and text. I'm trying to display text only if the image field is empty and display both image and text when they both exist in a row. However, i tried using the if statement in the while loop statement below, the problem is that when it suppose to display only text,it displays text with image tag with no src value,kindly help fix this.
Thanks.
while($row = mysqli_fetch_assoc($query)){
$id = $row["id"];
$text = $row["texts"];
$image =$row['images'];
if(!empty($text) && !empty($image)){
echo '<img src='.$image.'/>';
echo '<div>'.$text.'</div>';
}elseif( !empty($text) && empty($image) ){
echo '<div>'.$text.'</div>';
}
}
$row['images'] might contain spaces, try trimming it. Also, you can simplify the if:
while($row = mysqli_fetch_assoc($query)){
$id = $row["id"];
$text = trim($row["texts"]);
$image = trim($row['images']);
if(!empty($image)){
echo '<img src='.$image.'/>';
}
if(!empty($text)){
echo '<div>'.$text.'</div>';
}
}
while($row = mysqli_fetch_assoc($query)){
$id = $row["id"];
$text = $row["texts"];
$image =$row['images'];
if($image)
echo "<img src='{$image}' />";
if($text)
echo "<div>{$text}</div>";
}
Related
I'm trying to make a php slideshow and I'm almost done I just need to implement the next and back buttons which I thought were going to be easy, but apparently you can't increment indexes in php?
$sql = "SELECT pic_url FROM pic_info";
$result = $conn->query($sql);
$count = 0;
$dir = "http://dev2.matrix.msu.edu/~matrix.training/Holmberg_Dane/";
$source = "gallery.php";
if ($result->num_rows > 0) {
// output data of each row
$pic_array = array();
while ($row = $result->fetch_assoc()) {
$pic_array[$count] = $row['pic_url'];
$count++;
}
$index = 1;
echo "<img src= ' $dir$pic_array[$index]' />";
echo "<a href= '$dir$pic_array[$index + 1]'>next</a>";
echo "<a href= '$dir$pic_array[$index - 1]'>back</a>";
}
$conn->close();
?>
Try this
<?php
echo '<img src="'.$dir.$pic_array[$index].'">
next
back';
?>
I would suggest to place the url's in an array and loop over them.
$urls = ['website/url/for/image/image.jpg', 'another/url/image.jpg'];
foreach ($urls as $url) {
echo 'a href="http://'.$url.'"> Click </a>';
}
It is definitely more readable that way.
I need to generate mulitple qr code in the same webpage.
Which echo in a table,one row one qr code, and it is generated by calling echo 'img src = "generateqrcode.php"'.
Also I need to do some checking before printing each row the data and qr code.
However I found that the generated qr code were overwritten by the last one. Why this happened?
generateqrcode.php
session_start();
$key = $_SESSION['key'];
$link = TARGET_LINK.$key;
QRcode::png($link,false,"L",10,0);
A.php
$row=1;
while($row <=1){
echo '<table>';
echo '<td>';
echo $row;
echo '</td>';
echo '<td>';
$sql = GETSECTIONSQL;
if(!($qids = get_records_sql($query))){
$qids = array();
}
foreach($qids as $qid){
$qrsec = $qid->section;
if($section == $qrsec){
$sql2 = GETLINKSEQUENCESQL;
if(!($viewids = get_records_sql($query))){
$viewids = array();
}
foreach($viewids as $viewid){
$vid = $viewid->sequence;
session_start();
$_SESSION['vid'] = $vid;
echo '<td>';
echo '<a class="fancybox" href="generateqrcode.php">';
echo '<img src="generateqrcode.php"/></a>';
echo '</td>';
}
}
}
echo '</table>';
$row++;
}
New
A.php
foreach($viewids as $viewid){
$vid = $viewid->sequence;
echo '<td>';
echo '<a class="fancybox" href="generateqrcode.php">';
echo '<img src="generateqrcode.php?id=$vid"/></a>';
echo '</td>';
}
generateqrcode.php
$id = $_GET['id'];
$link = TARGET_LINK.$id;
QRcode::png($link,false,"L",10,0);
Well, all your images are linking to the exact same "image" generateqrcode.php. Of course they're all going to look the same. Since your session can only store one value at a time, you cannot transport any ids unique per image in the session. You should make unique links to unique images:
<img src="generateqrcode.php?id=1234567">
Then use $_GET['id'] when generating the image instead of the session value.
I have the following code which orders my data in mysql perfectly:
<?php
$con = mysql_connect('localhost','root','password');
mysql_select_db("users");
$pop = mysql_query("
SELECT * FROM images ORDER BY pop DESC LIMIT 9
");
while($row = mysql_fetch_array($pop))
{
echo $row['imagefile'];
echo "<br />";
}
mysql_close($con);
?>
However i would like for each "result" to then automatically be assigned as a variable. So for example "uploads/logo.png" comes out as the first "result" of the above code. I would then want this to be assigned $image_1 - so in code it would read $image_1 = "uploads/logo.png". I then want all the other 8 outputs to be assigned to variables so that $image_2 corresponds to the second output and so on. This is only a prototype. I wish for it to eventually output 100 results. Is this at all possible? Thanks a lot for your time.
Use an array.
$images = array();
while($row = mysql_fetch_array($pop))
$images[] = $row['imagefile'];
Or keep the associative array:
$imageData = array();
while($row = mysql_fetch_array($pop))
$imageData[] = $row;
// $imageData[0]['imageFile'] will be "uploads/logo.png"
Edit for comment below:
First method from above:
<?php
foreach ($images as $image) {
echo <<<EOT
<div class="box"><img src= "$image" width="200" height="150"></a></div>
EOT;
}
?>
Second method could be more involved, depending on your data:
<?php
foreach ($imageData as $image) {
$imagePath = $image['imageFile'];
$imageWidth = $image['width'];
$imageHeight = $image['height'];
echo <<<HTML
<div class="box"><img src= "$imagePath" width=$imageWidth height=$imageHeight></a></div>
HTML;
}
?>
I want to show a variable (which is an image) a certain amount of times depending on the number from a different column.
So I want to have $image shown $numberofratings times (which is up to 5). I'm pretty new to SQL, so I'm probably missing something quite basic, but thankyou to anyone who helps!
<?
$query = mysql_query("SELECT * FROM alex_demo23");
while ($row = mysql_fetch_array($query)){
$rating=$row['rating'];
$numberofratings=$row['numberofratings'];
$image = '<img src="images/star.png">';
echo ("addMarker(Rated: $rating $image from $numberofratings reviews');\n");
}
?>
Just use a for() or a str_repeat:
$image = '';
for($i=0; $i<$numberofratings; $i++){
$image .= '<img src="images/star.png">';
}
Or
$image = str_repeat('<img src="images/star.png">', $numberofratings);
This should do it:
$image = "";
for ($i = 0; $i < $row['numberofratings']; $i++) {
$image .= '<img src="images/star.png">';
}
There's no error checking to make sure that the data is valid, but it should be a start.
I have the following code:
while($row = mysql_fetch_array($result)){
$output_items[] = $row["title"]; } // while
print(implode("\n", $output_items));
Which does what it says and splits the array with a new line for each item.
But how do I do the same and allow formatting with i.e. I basically want to say
foreach of the $output_items echo "<div class=whatever>$output_items</div> etc etc
Tearing my hair out with this!
Many thanks for all help
Darren
foreach ($output_items as $oi){
echo "<div class=whatever>$oi</div>";
}
doesn't work? or i did not get what you are searching for
Pretty simple, to make it easier to read I'd do something like this:
while($row = mysql_fetch_array($result))
{
echo '<div class="whatever">';
echo $row["title"];
echo '</div>' . "\n";
} // while
Although you could still do this with your original code pretty easily:
while($row = mysql_fetch_array($result)){
$output_items[] = '<div class="whatever">' . $row["title"] . '</div>'; } // while
print(implode("\n", $output_items));
Rather than implode() them all with line breaks, use string interpolation to add them together:
$out_string = "";
// Loop over your array $output_items and wrap each in <div />
// while appending each to a single output string.
foreach ($output_items as $item) {
$out_string .= "<div class='whatever'>$item</div>\n";
}
echo $out_string;