How can we fetch images from database to dashboard using CodeIgniter 3 - codeigniter

Image path is getting stored in the database and file is getting uploaded in the upload path but the image is not displaying in the tabular format.
Here is the code for the view:
<tbody>
<?php
$query = $this->db->query("SELECT * FROM banner_mgmt LIMIT 1");
// $url = base_url().
foreach ($query->result() as $row)
{
echo "<tr>";
echo "<td>$row->id</td>";
echo "<td>$row->banner_name</td>";
echo "<td>".'<img src="base_url().$row->banner_image" style="height:150px; width:150px;" alt="not found">'."</td>";
echo "<td>$row->banner_image</td>";
echo "</tr>";
}
?>
</tbody>

Assuming that base_url().$row->banner_image does actually result in a correct path/image name...
You need to check your string concatenation.
What you have... (overly complicated)
echo "<td>".'<img src="base_url().$row->banner_image" style="height:150px; width:150px;" alt="not found">'."</td>";
What you should have...
echo '<td><img src ="'.base_url().$row->banner_image.'" style="height:150px; width:150px;" alt="not found"></td>';
You need to think about how you want the string to turn out, which helps you determine when and where to use ', " and . properly.
Here I am wrapping the string using ' (single quotes), which allows me to use " (double quotes) in the HTML properties without much effort.
Read up on how to use base_url() in the Codeigniter user guide...
So you could have
echo '<td><img src ="'.base_url($row->banner_image).'" style="height:150px; width:150px;" alt="not found"></td>';
You could even use " (double quotes) which evaluate php but you will need to escape any 'internal' double quotes you want to display.
echo "<td><img src =\"base_url($row->banner_image)\" style=\"height:150px; width:150px;\" alt=\"not found\"></td>";
So, you need to read the answer at What is the difference between single-quoted and double-quoted strings in PHP?

Related

GET path not accepting 'dot' in it

I have router path with a '.' in it, my path is downloads/pass.pkpass
I have defined the path in web.php as the following
$router->get('downloads/pass.pkpass', 'PassServerController#downloadPass');
but somehow it's not working. if I remove the '.' it's working fine. what might be the problem here?
It's was working before recently I updated lumen after that it's not working.
The . is the string concatenation operator in PHP.
example
<?php
$string1 = "Test";
$string2 = "working!";
$string = $string1 . $string2;
echo $string;
?>
this will print "Test working!"
You can get your route by this may be
$router->get('downloads/pass.'.'pkpass', 'PassServerController#downloadPass');
But this is some kind of hack we can say.
You can use _ instead of . parameter that will work for sure.

Displaying an img from database using Codeigniter

Is there any way to display an BLOB image from database ?
I've tried this but not working:
<?print'<img src="data:image/jpeg;base64,. base64_encode($img["Imag"])"> '; ?>
echo '<img src="data:image/jpeg;base64,'.base64_encode( $img['Imag'] ).'"/>';
Try this
There is problem in your concatination also don't use print in this scenario use echo.
echo '<img src="data:image/jpeg;base64,'. $img['Imag'] .'"/>';
OR
echo '<img src='.base64_encode ($img ["Imag"].' />';

Pass a variable to a template .phtml block in Magento

This code is write in market.phtml
<?php echo $this->getLayout()->createBlock('core/template')->setData('vendorId',$vendor->getCustomerId())->setTemplate('marketplace/vendors/badge.phtml')->toHtml();?>
In Badge.php
echo $this->vendorId;
But my output is null. Is this correct way to pass data to block?
You need to change your variable like this and check it
<?php echo $this->getLayout()->createBlock('core/template')->setVendorId($vendor->getCustomerId())->setTemplate('marketplace/vendors/badge.phtml')->toHtml();?>
Now you can access this vendor ID variable in badge.phtml file like this :
<?php echo $this->getVendorId();?>

write csv file to server with codeigniter csv_from_result

Trying to save a csv file on my server with codeigniter.
my syntax is:
$this->load->dbutil();
$query = $this->db->query("SELECT id,customeraccount,band FROM Customer_Bands");
$delimiter = ',';
$newline = "<br>";
echo $this->dbutil->csv_from_result($query, $delimiter, $newline);
if ( ! write_file(base_url().'/application/authorizations/test.csv', $this->dbutil->csv_from_result($query, $delimiter, $newline)))
{
echo 'Unable to write the file';
}
else
{
echo 'File written!';
}
This is rough right now but want to get functionality working first.
Two issues.
Firstly the echo $this->dbutil->csv_from_result($query, $delimiter, $newline); outputs to my view correctly but each field is inside "", how can I remove these?
Secondly, the file is not writing to my server.
base_url() is my codeigniter root folder then inside applications I have created a folder named authorizations so base_url()/application/authorizations
This currently fails and echos 'Unable to write the file'
Thanks in advance

fopen with dynamic filename is not working

I need to make a web site on which people can upload data files that after some treatment will be plotted using the jpgraph. The file is analyzed with a bash script called analfile.sh. The bash script is like this:
#!/bin/bash
file=$1
fecha=`date +%s`
mv $1 $fecha.dat
echo $fecha.dat
So, it gives back another file which name is like: 1321290921.dat. That is the file that I need to plot.
This my current php code:
$target_path = "/home/myhome";
$target_path = $target_path . basename( $_FILES['rdata']['name']);
$target_file = basename( $_FILES['rdata']['name']);
if(move_uploaded_file($_FILES['rdata']['tmp_name'], $target_path)) {
echo "The file ". $target_file. " has been uploaded";
chdir('/home/myhome');
$filetoplot=shell_exec('./analfile.sh'.' '.$target_file);
} else{
echo "There was an error uploading the file, please <a href=\"index.html\">try again!
</a>";
}
//$filetoplot="1321290921.dat"
echo "<br>The file to be opened is ". $filetoplot. "<br>";
if ($file_handle = fopen("$filetoplot", 'r')) {
while ( $line_of_text = fgets($file_handle) ) {
$parts = explode('.', $line_of_text);
echo $line_of_text ;
print $parts[0] . $parts[1]. $parts[2]. "<br>";
}
fclose($file_handle);
}
I have permissions to read and write on the target directory. I find strange that if I uncomment the line $filetoplot="1321290921.dat" then the script works perfectly. I guess I am doing something stupid since this is my first code in php but after some hours googling I was not able to find a solution.
Any help will be appreciated.
First thing I'm seeing is that you don't append trailing slash (/) to your home path, so that the path would be like /home/myhomefoo rather than /home/myhome/foo.
You should also move $target_file earlier, and reuse that within $target_path. There's no reason to do the same thing twice.
If that doesn't help, we'll see what goes next.

Resources