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.
Related
I'm trying to make this code insert a line break after 2 images when there are a total of 4 images. The Code listed below adds the line break after all images are displayed instead of after 2 images. Could someone please help me with this?
$images = "";
$i = 0;
while ($info = mysqli_fetch_array($imglist)) {
$style = $info['Style'];
$imgpath = $info['ImgPath'];
$standardimg = $info['StandardImg'];
$colorname = $info['ColorName'];
$smallimgwidth = $info['SmallImgWidth'];
$images = $images.
"<img src='$imgpath/$standardimg-Small.jpg'
alt = '$mill $style - $colorname'
title = '$mill $style - $colorname'
style = 'min-width:35px; max-width:$smallimgwidthpx;' / > ";
if (mysqli_num_rows($imglist) == 4) {
if ($i != 0 && $i % 2 == 1) {
echo '<br />';
}
}
$i++;
}
**Problem **
You are constructing the tags dynamically but in the process you are adding tags separately.
Solution
Add tag to your tags string being generated.
Code
$images = $images . '<br />';
In place of
echo '<br />';
Change
echo '<br />';
to
$images = $images . '<br />';
Just like that.
I little explanation: you're not 'grouping' br output with image output, since one goes into a variable (image) and the other is displayed immediately (br).
I'm stuck on a massive project I'm helping with- we have a FileMaker database and I've created an online catalogue for it. Originally we just had clothing and it worked perfectly, but now we're adding other collections as well. I'm trying to make just the the "C" (clothing) collection show up below, but it's not working. I'm new to the FileMaker api so help is greatly appreciated.
<?php
/*the function that displays the buttons (the img) of each dress to link to the details page*/
function displayRecords($records){
/*TO DO*/
foreach ($records as $record){
$photo = $record->getField("Photo");
$thumbnail = $record->getField("Thumbnail");
$date = $record->getField("Date CR");
$tNum = $record->getField("Catalog_Number");
$category = $record->getField("Collection");
if ($category = "C"){
echo ("<div class=\"dimg\">");
echo ("<a href = \"http://fadma.edu/historicalcollection/museum/details_test_textiles.php?id=");
echo($tNum);
echo ("\">");
echo ("<img src= \" ");
echo ($thumbnail);
echo (" \"></a>");
echo ("<div class=\"desc\">");
echo ($date);
echo ("</div></div>");}
}
}
$begin = (int)$_GET["begin"];
$end = (int)$_GET["end"];
for ($x = $begin; $x <= $end; $x++){
$findCommand = $fm->newFindCommand("Listing");
$findCommand->addFindCriterion("Photo", "*");
$findCommand->addFindCriterion("Date CR", $x);
$result = $findCommand->execute();
if(FileMaker::isError($result)){
continue;
}
$records = $result->getRecords();
displayRecords($records);
}
?>
I typically use fx.php for querying Filemaker but after a quick look at your code you seem to be filtering the results with the following:
if ($category = "C"){
However the single = is an assignment operator, not a comparison operator. You will want to check if $category is equal to C, not set $category to C.
Try using if ($category == "C"){ instead. Note the double ==
im trying to use a foreach loop with the plugin fotorama.
What im trying to do is load one half sized image for the main gallery image. Which i have working in a foreach, but i want to use a full image for the data-full tag but i cant get it to work.
This is the working code.
<div class="fotorama"
data-allowfullscreen="native"
data-nav="thumbs"
data-fit="scaledown"
data-width="100%"
data-height="100%"
data-arrows="true"
data-click="true"
data-swipe="true">
<?php
$dirname = "admin/image-upload/uploads/";
$images = glob($dirname."*.*");
foreach($images as $image) {
echo '<img src="'.$image.'" /><br />';
}
?>
</div>
this is what im trying to do.
<div class="fotorama"
data-allowfullscreen="native"
data-nav="thumbs"
data-fit="scaledown"
data-width="100%"
data-height="100%"
data-arrows="true"
data-click="true"
data-swipe="true">
<?php
$dirname = "admin/image-upload/uploads/";
$images = glob($dirname."*.*");
$dirname2 = "admin/image-upload/full/";
$images2 = glob($dirname2."*.*");
$fullImgs = "<img data-full=".$image2." src=".$image." /><br />";
foreach($fullImgs as $fullImg) {
echo $fullImg;
}
?>
</div>
thanks in advanced guys
Try this:
$dirname = "admin/image-upload/uploads/";
$images = glob($dirname."*.*");
$dirname2 = "admin/image-upload/full/";
$images2 = glob($dirname2."*.*");
$array = array_merge($images, $images2);
// Supossing both array have same length
$length = count($images);
for($i = 0; $j = $length; $i < $length; $i++, $j++) {
echo '<img data-full=".$images2[$j]." src=".$images[$i]." /><br />';
}
I think what you want is this:
<?php
$dirname = "admin/image-upload/uploads/";
$images = glob($dirname."*.*");
$dirname2 = "admin/image-upload/full/";
$images2 = glob($dirname2."*.*");
//assuming $images and $images2 are the same length...
foreach ($images as $k => $v) {
echo "<img data-full=".$images2[$k]." src=".$v." /><br />";
}
?>
Haven't tested, but should give the idea...
Your code won't work like this. If I understood you correctly you have the same image in big and small format in two different directories. To show them as pair you need to make a connection between those two files - because how should your script know which images belong together? You could use a database, but in your case I think it is easier to make a connection in the filename. For example, name the pictures in the directory for the small images
Image_7263.png
Image_0172.png
And so on. For the big images you simply append _BIG to the end of the name.
Then you use your foreach loop to loop through the directory for the small images. For each image in there, you append _BIG to the end of the filename and include it from the directory for the big ones.
$smallImages = glob ("/path/to/small/images/*.*");
foreach ($smallImages as $img)
{
$name = substr ($img, 0, strlen ($img)-4); //Remove the .png or .jpg
$bigImg = "/path/to/big/images/".name."_BIG.jpg"; // or whatever image type you are using
echo "<img data-full=\"".$bigImg."\" src=\"".$img."\" />
}
I have a piece of code that pulls a set of images from a directory.
Problem is, it allows duplicates which I do not want.
Anybody an idea how to let every image be unique?
Can’t seem to figure it out right away with my limited knowledge. thanks!
<?php
$files = glob("images/*.*");
for($x = 0 ; $x < 4; $x++)
{
$fileNum=rand(1, count($files));
$image = $files[$fileNum];
echo '<img src="'.$image .'" id="lay"/>';
}
?>
It looks like you want four random files from the array to echo out. I suggest you use array_rand(4) to get four random keys from the list.
$files = glob("images/*.*");
$keys = array_rand(4);
foreach ($keys as $key) {
$image = $files[$key];
echo '<img src="'.$image .'" id="lay"/>';
}
Try this instead:
<?php
$files = glob("images/*.*");
foreach ( $files as $file )
{
echo '<img src="'. $file .'" id="lay"/>';
}
?>
Hope I could help you!
$images = array();
$images[0][0] = "boxes/blue.jpg";
$images[0][1] = "blah.html";
$images[1][0] = "boxes/green.jpg";
$images[1][1] = "blah.html";
$images[2][0] = "boxes/orange.jpg";
$images[2][1] = "blah.html";
$images[3][0] = "boxes/pink.jpg";
$images[3][1] = "blah.html";
$images[4][0] = "boxes/purple.jpg";
$images[4][1] = "blah.html";
$images[5][0] = "boxes/red.jpg";
$images[5][1] = "blah.html";
$images[6][0] = "boxes/yellow.jpg";
$images[6][1] = "blah.html";
$i = 0;
*echo "<a href='" . $images[0][1] . "'><img src='" . $images[0][0] . "' /></a>";
$boxes = array();
while($i<5)
{
$rand = rand(0,(sizeof($images)-1));
//echo $rand;
$slice = array_splice($images, $rand);
$boxes[$i] = $slice;
$i++;
}*
I am trying to get a random image picker to choose from a list of images provided by the $images array. However, I am unable to fill the $boxes array with anything other than "Array". Can anyone tell me why? Any help is much appreciated
UPDATE
I am now using the code below and it breaks whenever it comes across an empty element. Unless i am very much mistaken, shouldn't splice patch up holes like that?
$rand = rand(0,(sizeof($images)));
array_splice($images, $rand);
$i = 0;
while($i<5)
{
echo "<a href='" . $images[$i][1] . "'><img src='" . $images[$i][0] . "' /></a>";
$i++;
}
This might be a nicer way of doing it:
foreach (array_rand($images, 5) as $key) {
$boxes[] = $images[$key];
}
Slightly off topic, but wouldn't it be easier in this case (picking 5 items from a list of 6) just to pick one element and discard it from the original array, and then use the original? This will also ensure you do not get duplicates in the resultant array.
I realise that you may have more than 6 items in the original, and may want less than 5 from it, but I'm talking specifically about the example posted.
array_splice() returns an array.
You can try something like this:
while($i<5)
{
$rand = rand(0,(sizeof($images)-1));
$boxes[$i] = $images[$rand];
$i++;
}