I'm trying to display image in 5 by 3 table.
I'm able to display the images if all empty(blank.png).
Here is the code
<?PHP
$ds ='\image';
$imagefile = array("EX_W1_01.png", "EX_W1_02.png", "EX_W2_01.png","EX_W3_01.png");
echo "<pre>"; print_r($imagefile);
$file = 'blank.png';
$d = $ds.$file;
echo "<table border = 1 width=\"540px\" cellspacing=\"0px\" cellpadding=\"0px\">";
for($row=1;$row<=5;$row++){
echo "<tr>";
for($col=1;$col<=3;$col++){
// echo"<td height=60px>W$row</td>";
//if()
echo"<td height=60px>W$row<img border = 1 height = 120 width = 120 src = $d ></td>" .PHP_EOL;
}
echo "</tr>";
}
echo "</table>";
?>
I want to display the images base on middle file name array $imagefile eg W1, W2 and if not in array, I will display the blank.png.
I was able to get the middle file name by this code, but I cannot display the images in correct row/col.
for($i=0;$i<count($imagefile); $i++) {
$wd = substr($imagefile[$i], 3, strpos($imagefile[$i], '_'));
}
Can you try this,
Based on your code add these when you echo the image,
$wd = substr($imagefile[$i], 3, strpos($imagefile[$i], '_'));
if($wd == *the increment either row or col*)
{
echo"<td height=60px>W$row<img border = 1 height = 120 width = 120 src = $d ></td>" .PHP_EOL;
}
else
{
echo"<td height=60px>No image</td>" .PHP_EOL;
}
See if it works.
Here it is
<?php
$ds ='/image';
$imagefile = array("EX_W1_01.png", "EX_W1_02.png", "EX_W2_01.png","EX_W3_01.png");
//echo "<pre>"; print_r($imagefile);
$default = 'blank.png';
?>
<table border = 1 width=\"540px\" cellspacing=\"0px\" cellpadding=\"0px\">
<?php
for($row=1;$row<=5;$row++){
?>
<tr>
<?php
for($col=1;$col<=3;$col++){
// construct the file name
$filename = 'EX_W' . $row . '_0' . $col . '.png';
// set the default image file
$imgPath = $ds . '/' . $default;
// in case the file name exists in your array with images,
// set the correct path to the image
if (in_array($filename, $imagefile)) {
$imgPath = $ds . '/' . $filename;
}
?>
<td height=60px>
<img border="1" height="120" width="120" src="<?php echo $imgPath; ?>"/>
</td>
<?php } ?>
</tr>
<?php
}
?>
</table>
As you can see I also prefer to "embed" the php code in the HTML. It makes no sense to me outputting HTML code through the PHP engine if it can be parsed as is ;-)
Related
I'm trying to display images from database but unable to show .
Please help me.
My code
$id = $_GET['reg_id'];
$sql13 = "select * from contacts where reg_id=" . $id;
$result13 = mysqli_query($conn, $sql13);
if (mysqli_num_rows($result13) > 0) {
while($documents = mysqli_fetch_array($result13))
{ ?>
<li class="make_text1" style="font-size:16px">
<span class="definition"><b>
<?php if(!empty($documents["name"])){ echo
$documents["name"]; }?><br><?php
if(!empty($documents["image"])){
$upload_dir = 'uploads/';
// echo "<img src='uploads/".$documents["image"]."' width='800' height='500'> ";
echo "<img src='uploads/".$documents["reg_id"]."/".$documents["image"]."' width='800' height='500'> ";
}
You need to loop over the values in $documents["image"]. Try this:
$images = explode(',', $documents["image"]);
foreach ($images as $image) {
echo "<img src='uploads/".$documents["reg_id"]."/$image' width='800' height='500'> ";
}
or if you only want do display one of them e.g. the first, something like this:
$images = explode(',', $documents["image"]);
echo "<img src='uploads/".$documents["reg_id"]."/".$images[0]."' width='800' height='500'> ";
I want to make a product gallery like 5 products, each has their own background-image attribute
I use loop to insert the product image, so I want to make it each loop will have different background-image
I'm thinking of using IF statement like so
<?php
$bg = 0;
$bg1 = "url('img1.jpg')";
$bg2 = "url('img2.jpg')";
$bg3 = "url('img3.jpg')";
$bg4 = "url('img4.jpg')";
$bg5 = "url('img5.jpg')";
if ($bg = 0){
echo " <div style='background-image :$bg1 ;'>" ;
$bg = 1;
} else if ($bg= 1) {
echo " <div style='background-image :$bg2 ;'>" ;
$bg = 2;
} else if ($bg= 2 ) {
echo " <div style='background-image :$bg3 ;'>" ;
$bg = 3;
} else if ($bg= 3 ) {
echo " <div style='background-image :$bg4 ;'>" ;
$bg = 4;
} else if ($bg= 4 ) {
echo " <div style='background-image :$bg5 ;'>" ;
$bg = 0;
}
echo " </div> " ;
code for product images
?>
above is the simplified code I wrote, it doesn't work.
if anyone has a different but much simpler solution it will be appreciated
note : the image files are in the same directory with this php file
thank you
Would you be open to using img tags? This, in my opinion, would be a better solution:
Code:
<?php
$images=Array(
"img1.jpg",
"img2.jpg",
"img3.jpg",
"img4.jpg",
"img5.jpg"
);
//
print "\n<br> Code: \n<pre>\n".RenderThoseImages($images)."\n</pre>";
//
function RenderThoseImages($images)
{
//
$s="";
//
foreach($images as $image){
$s.="\n<div><img src=\"{$image}\"></div>";
}
return $s;
}
?>
Outputs:
<br> Code:
<pre>
<div><img src="img1.jpg"></div>
<div><img src="img2.jpg"></div>
<div><img src="img3.jpg"></div>
<div><img src="img4.jpg"></div>
<div><img src="img5.jpg"></div>
</pre>
The main reason being that when you use the background-image CSS, you're also responsible for grabbing the image dimensions in PHP and rendering height/width into the div CSS as well, or possibly creating some javascript to fix it after loading, creating unneeded headache.
Can u pls tell me how to bind multiple images in powerpoint using php powerpoint library? In the below code i used foreach loop to add multiple images in powerpoint but only one image is adding so pls help me.
<?php
require_once("db_config.php");
set_include_path(get_include_path() . PATH_SEPARATOR . 'Classes/');
include 'PHPPowerPoint.php';
include 'PHPPowerPoint/IOFactory.php';
?>
<html>
<h3 align="center">Welcome <?php echo $_SESSION['user_name'];?>
<br>
Logout
</h3>
<body>
<?php
echo "<div align='center'>";
echo "<form method='post' action=''>";
echo "<table align='center' border='1'>
<tr>
<th></th>
<th>ID</th>
<th>Firstname</th>
<th>Lastname</th>
<th>UserName</th>
</tr>";
$result = mysql_query("SELECT * FROM users");
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td><input type='checkbox' name='user_id[]' value='".$row['user_id']."' /> </td>";
echo "<td>" . $row['user_id'] . "</td>";
echo "<td>" . $row['first_name'] . "</td>";
echo "<td>" . $row['last_name'] . "</td>";
echo "<td>" . $row['user_name'] . "</td>";
echo "</tr>";
}
echo "</table><br>";
echo "<input type='submit' name='submit' value='Export PPT' />";
echo "</form></div>";
?>
</body>
</html>
<?php
if(isset($_POST['submit']) && isset($_POST['user_id'])){
$user_id = $_POST['user_id'];
foreach($user_id as $selected){
echo $selected."</br>";
$result = mysql_query("SELECT * FROM users WHERE user_id ='".$selected."'");
$row = mysql_fetch_array($result);
$img = '<img src="images/'.$row["image_name"].'" height="200" width="300" />';
$objPHPPowerPoint = new PHPPowerPoint();
// block sets slide logo.
$currentSlide = $objPHPPowerPoint->getActiveSlide();
$shape = $currentSlide->createDrawingShape();
$shape->setPath('images/'.$row["image_name"]);
$shape->setWidth(640);
$shape->setHeight(480);
$shape->setOffsetX(10);
$shape->setOffsetY(10);
// block sets text for first slide.
$shape = $currentSlide->createRichTextShape();
$shape->setHeight(700);
$shape->setWidth(600);
$shape->setOffsetX(10);
$shape->setOffsetY(500);
$shape->getAlignment()->setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_CENTER );
$textRun = $shape->createTextRun('FirstName:'.$row["first_name"].' Lastname:'.$row["last_name"].' UserName:'.$row["user_name"]);
$textRun->getFont()->setBold(true);
$textRun->getFont()->setSize(30);
$textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( '#FFFF' ) );
// block sets text for first slide ends.
$filename = str_replace('.php', '.pptx', __FILE__);
$newname = "PresentationReport-" . date('Y-m-d-H-i-s') . ".pptx";
$objWriter = PHPPowerPoint_IOFactory::createWriter($objPHPPowerPoint, 'PowerPoint2007');
$objWriter->save(str_replace('.php', '.pptx', __FILE__));
// block to download file.
header("Pragma: no-cache");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment;filename=" . $newname);
ob_clean();
flush();
readfile($filename);
exit();
}
}
else
{
echo "<div align='center'>Please choose the user</div>";
exit;
}
?>
How can i add multiple images using php powerpoint library?
Two troubles can be indentified in your snippet:
1 - You're calling "new Powerpoint" to each element in foreach, and due this
you always override the previous slide.
2 - You're positioning your shapes in the same x/y offset:
$shape->setOffsetX(10);
$shape->setOffsetY(10);
So in the foreach all shapes will be stacked.
To a functional solution you'll need to mount a valid algorithm like:
I'll have 4 photos by slide for example so:
$resultsFromDataBase = $resultsFromDataBase;
// Array chunk to create a "logic" 4 photos ... jump to the new slide
$resultsBySlide = array_chunk($resultsFromDataBase, 4);
// Each photo needs your independent X offset
// Note the fake values, you need to calculate them
$positionsByIndex = [
0 => 10,
1 => 20,
2 => 30,
3 => 40
];
foreach ($resultsBySlide as $i => $rows) {
// 4 photos -> Jump the slide and create another one
$ppt->createSlide();
// SlideCount -1 gets your current index
$slideIndex = $ppt->getSlideCount()-1;
$currentSlide = $objPHPPowerPoint->getActiveSlide();
foreach ($rows as $position => $row) {
$shape = $currentSlide->createDrawingShape();
$shape->setPath('images/'.$row["image_name"]);
$shape->setWidth(640);
$shape->setHeight(480);
$shape->setOffsetX($positionsByIndex[$position]); // Here the point
$shape->setOffsetY(10);
}
}
And the last point but not less important: Don't use mysql_* in your project
use: PDO or mysqli_* instead
I am using the following code to populate images from MySQL database. I want to have image_title for every image which will be a textbox below the image. I know how to populate it but how can I make it named so when user changes the title it will only change for the specific image?
<?php
$mysql_command = "CALL sp_populate_member_article_media(:param_article_guid, :param_member_id)";
$mysql_query = $mysql_connection->prepare($mysql_command);
$mysql_query->bindParam(':param_article_guid', $_GET["aid"], PDO::PARAM_STR);
$mysql_query->bindParam(':param_member_id', $_SESSION["volow_member_id"], PDO::PARAM_STR);
$mysql_query->execute();
while($mysql_row = $mysql_query->fetch())
{
list($width, $height) = getimagesize($_SESSION["volow_absolute_path"] . "/gallery/members/" . $_SESSION["volow_member_guid"] . "/" . $_SESSION["volow_channel_guid"] . "/" . $_GET["aid"] . "/". $mysql_row['file_name'] . ".png");
if ($width > 650) { $width = 650; };
if ($height > 271) { $height = 271; };
?>
<tr><td align="right" valign="top"><img src="<?php echo $_SESSION["volow_domain_name"]; ?>gallery/members/<?php echo $_SESSION["volow_member_guid"]; ?>/<?php echo $_SESSION["volow_channel_guid"]; ?>/<?php echo $_GET["aid"]; ?>/<?php echo $mysql_row['file_name']; ?>.png" width="<?php echo $width; ?>" height="<?php echo $height; ?>" /></td></tr>
<tr><td><hr></td></tr>
<?php } ?>
You need to give a specific name for input files of each image. You need a unique identifier for each image for this purpose.
Assuming you have file_id field for each image, you can generate form elements as follows:
<input type="text" name="title<?=$mysql_row['file_id']?>" />
Then when handling form submission, you need to iterate over $_POST array to find out which image ids were submitted:
foreach ($_POST as $name => $value) {
if (substr($name, 0, 5) == 'title') {
$id = (int) substr($name, 5);
$title = $value;
$sql = "UPDATE imagetable SET title = " . mysqli_real_escape_string($title) . " WHERE id = " . $id;
...
// run sql
...
}
}
I have the working script below but instead of image 1, image 2, image 3, image 4 being next to the boxes I want different names for each. Is there an easy way to do this?
Thanks in advance!!
Also where would I put
header("Location: thankyou.php");
exit();
Because at the moment after putting this in, it just directs straight to the thankyou.php not the page below (document.php)
<?php
$max_no_img = 4; // Maximum number of images value to be set here
echo "<form method=post action='' enctype='multipart/form-data'>";
echo "<table border='0' width='400' cellspacing='0' cellpadding='0' align=center>";
for ($i = 1;$i <= $max_no_img;$i++) {
echo "<tr><td>Images $i</td><td>
<input type=file name='images[]' class='bginput'></td></tr>";
}
echo "<tr><td colspan=2 align=center><input type=submit value='Add Image'></td></tr>";
echo "</form> </table>";
while (list($key, $value) = each($_FILES['images']['name'])) {
//echo $key;
//echo "<br>";
//echo $value;
//echo "<br>";
if (!empty($value)) { // this will check if any blank field is entered
$filename = rand(1, 100000) . $value; // filename stores the value
$filename = str_replace(" ", "_", $filename);
$add = "upload/$filename"; // upload directory path is set
copy($_FILES['images']['tmp_name'][$key], $add);
echo $add;
// upload the file to the server
chmod("$add", 0777); // set permission to the file.
}
}
?>
</body>
</html>
After this line:
$max_no_img = 4;
Define an array with the names you're willing to give to each image:
$imgs_names = array('name for first image' , 'name for second image' , 'name for third image'); //and so on...
and instead of:
echo "<tr><td>Images $i</td><td>
write:
echo "<tr><td>Images ".$imgs_names[$i-1]."</td><td>
About using header , there's a problem since you already used echo.
Add ob_start() at the beginning of the file and ob_flush() at the end of the file,
now you can add the header() even after sending output.
EDIT2: Regarding your comment , there's an alternative way for redirection.
Add:
$submit = true;
After:
chmod("$add", 0777); // set permission to the file.
And after:
}
}
Add:
if(isset($submit) && $submit)
{
echo '<meta http-equiv="refresh" content="0; url=http://www.yoursite.com/thankyou.php">';
}