Ordering and then setting variables - php

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;
}
?>

Related

Want to post multiple search results

I tried to make a search function, when your search a tag, its supposed to give you the images with that tag. When i just echo in the php code (line 101), it gives all the links. However, when i try to post it in the html, it only gives one result back.
php code
$postTags = "";
if (isset($_POST['Find'])) {
try {
$pdoConnect = new PDO("mysql:host=localhost;dbname=imdterest", "root", "");
} catch (Exception $exc) {
echo $exc->getMessage();
exit();
}
$postTags = $_POST['naam'];
$pdoQuery = "SELECT * FROM posts WHERE postTags = :tags";
$pdoResult = $pdoConnect->prepare($pdoQuery);
$pdoExec = $pdoResult->execute(array(":tags" => $postTags));
while ($row = $pdoResult->fetch(PDO::FETCH_ASSOC)) {
$postTags = $row['postImageUrl'];
//echo $postTags;
}
}
html code
<div class="search">
<img src="<?php echo $postTags; ?>">
</div>
the problem is that in your php code you have WHILE loop and you echo link for all found rows. That is why it prints all results. But also you don't store the values but overwrite it every while iteration. In your HTML you don't have while loop and you just print overwritten variable (probably last result).
You can either store the results in an array or move while loop to HTML location.
Storing into array:
$allResults = array();
while ($row = $pdoResult->fetch(PDO::FETCH_ASSOC)) {
$allResults[] = $row['postImageUrl'];
}
and then somewhere in your html code
<div class="search">
<?php
foreach ($allResults as $imageLink){
echo '<img src="' . $imageLink . '">";
}
?>
</div>
The variable $postTags is getting overwritten and will hold only last value of MySQL result set. Create an array and loop over it.
$postTags = array();
while ($row = $pdoResult->fetch(PDO::FETCH_ASSOC)) {
$postTags[] = $row['postImageUrl'];
}
In HTML:
foreach($postTags as $val)
{ ?>
<div class="search">
<img src="<?php echo $val; ?>">
</div>
<?php
} ?>
Change your while loop like this, As you are not using array next data will over write the previous and all you are left with one image link that is last image link. hence to prevent overriding use array.
$postTags=[];
while ($row = $pdoResult->fetch(PDO::FETCH_ASSOC)) {
$postTags[] = $row['postImageUrl'];
//echo $postTags;
}
And html code like this , As now you have multiple images you need to display all and for that you need to iterate and foreach is one method to iterate through array.
<?php
foreach ($postTags as $postTag) {
?>
<div class="search">
<img src="<?php echo $postTag; ?>">
</div>
<?php }?>

echoing multiple image urls out of the db

I am trying to echo out multiple image urls from one column from the database. My echo script which echo's out the numbers of images that is coming from the db
however it can't get the main image for each image out from the folder only showing boxes that are equivalent to the db image paths this is my code.Thanks in Advance
Note:commas where used to seperate the images while inserting image path to the db
<?php
$q="praise"; //Folder path
$get_pro="SELECT * FROM `a`";
$run_pro = mysqli_query($con, $get_pro);
while($row_pro=mysqli_fetch_array($run_pro)){
$a=$row_pro['q'];
$str= $row_pro["image"];
$array = explode(',', $str);
foreach ($array as $item) {
echo "<img src=\"".$q."/".$item . "\" height=\"200\" width=\"200\"/>"; //only shows image boxes expecting images but calling from the folder is the problem
}
}
?>
try this If your image has absolute path
<?php
$q="praise"; //Folder path
$get_pro="SELECT * FROM `a`";
$run_pro = mysqli_query($con, $get_pro);
while($row_pro=mysqli_fetch_array($run_pro)){
$a=$row_pro['q'];
$str= $row_pro["image"];
$array = explode(',', $str);
foreach ($array as $item) {
// absolute path of the image
echo "<img src='".$q.DIRECTORY_SEPARATOR.$item."' height='200' width='200'/>";
}
}
?>
Do you know you have praise use two times in source as $q and in $item ? Try to compare src of image with path to image on server.
would you please try that
<?php
$q="praise"; //Folder path
$get_pro="SELECT * FROM `a`";
if($run_pro = mysqli_query($con, $get_pro)) {
while($row_pro=mysqli_fetch_assoc($run_pro)){
$a = $row_pro['q'];
$str = $row_pro["image"];
$array = explode(',', $str);
}
foreach ($array as $item) {
?>
<img src="<?php echo $q."/".$item; ?>" height="200" width="200" />
<?php
}
} else {
echo "error failed to run mysql query";
}
?>

implementing next and back buttons for a slideshow

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.

Display a multi image(array) name from database in codeigniter

My problem is how to display the array image from database. I called that array image because when I uploaded that image I'm using an array to do upload to database.
I'm using this code to insert the file name to database
public function multipost()
{
$filepath = $this->upload->get_multi_upload_data('file_name');
$filenames = '';
foreach($filepath as $a) {
$filenames .= $a['file_name'].",";
}
$filenames = rtrim($filenames,',');
$db_data = array(
'pic_id'=> NULL,
'ad_pic'=> $filenames,
);
$this->db->insert('technical_slide_img', $db_data);
}
That result to
As you can see the ad_pic column has the value of 1.jpg,2.jpg.
if I'm using like this
<?php foreach ($this->header_model->getalldata() as $row) {
$image_arr = explode("/", $row->image);
$image_name = end($image_arr);
echo base_url().'images/'.$image_name;
} ?>
to display that image. Is there any way to display that images? Or do I need to separate that 2 images into row?
replace
<?php foreach ($this->header_model->getalldata() as $row) {
$image_arr = explode("/", $row->image);
$image_name = end($image_arr);
echo base_url().'images/'.$image_name;
} ?>
with
<?php
foreach ($this->header_model->getalldata() as $row)
{
$image_arr = explode(",", $row->ad_pic);
foreach($image_arr as $image_name)
{
echo base_url().'images/'.$image_name .'<br />';
}
}
?>
As i see in your database table image is saved with comma , and you are exploding it with slash / . So i think you have to replace / with , in your explode function.
<?php foreach ($this->header_model->getalldata() as $row) {
$image_arr = explode(",", $row->image);
$image_name = end($image_arr);
echo base_url().'images/'.$image_name;
} ?>
<?php
$image_arr = explode(",", $data_user->image);
foreach($image_arr as $image_name)
{?>
<img style="width:5em;"src="<?php echo base_url().'uploads/'.$image_name?>" class="img-reponsive thumbnail">
<!-- echo base_url().'images/'.$image_name .'<br />'; -->
<?php }
?>

Getting last record value only

I am getting only the last element value, like in my case i am trying to get value of $dwnld_name
but don't know where i am missing, so only getting download name for the last record
<?php
global $cat_id;
$dwnld_sql = "SELECT * FROM wp_dm_downloads";
$dwnld_qry = mysql_query($dwnld_sql);
while($dwnld_row = mysql_fetch_array($dwnld_qry)){
echo $link = $dwnld_row['link'];
echo $dwnld_name = $dwnld_row['name'];
$cat_id = $dwnld_row['category'];
}
?>
<?php
$sql = "SELECT * FROM wp_dm_category";
$myquery = mysql_query($sql);
echo '<ul>';
while($row = mysql_fetch_array($myquery)){
$x = $row['name'];
echo $x_id = $row['id'];
?>
<li><a href="#"><?php echo $x; ?>
<?php if($x_id == $cat_id) { ?>
<ul>
<li><?php echo $dwnld_name; ?></li>
</ul>
<?php } ?>
</a></li>
<?php echo "<br/>";
}
echo '</ul>';
?>
It is because, you are taking the values from loop in strings.
Everytime loop runs, it overwrites the values by recent values.
You can create an array and append values to that and loop through that array using foreach
Corrected Code:
<?php
global $cat_id;
$dwnld_sql = "SELECT * FROM wp_dm_downloads";
$dwnld_qry = mysql_query($dwnld_sql);
$downloads = array();
while($dwnld_row = mysql_fetch_array($dwnld_qry)){
$downloads['link'] = $dwnld_row['link'];
$downloads['dwnld_name'] = $dwnld_row['name'];
$downloads['cat_id'] = $dwnld_row['category'];
}
?>
Now, loop through $downloads array.
Note: Do not use mysql_ functions are they are deprecated and will be removed in future versions of PHP.
You need to echo $dwnld_name in the loop, or save names into array.
With this solution you overwrite $dwnld_name variable in each loop and as a result after the loop you have the last record.

Categories