• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calling img src and alt from database

#1
Hi Everyone,

I've just started using CodeIgniter and have been enjoying it so far.

I have set a view up which is calling content from a database table and everything displays as expected for the text content.

However, I can't find a way to pass the img src and the alt description from the database table (image src in one column and alt in a separate column).

I can display images by calling the image direct from the /assets/images folder prefixed with "echo base_url" so I know it is the string I'm getting wrong.

I've tried searching the forum to no avail and Google examples haven't worked.

The view I have is:

Code:
<h2><?php echo $title; ?></h2>

<?php foreach ($accessories as $accessories_item): ?>

<div class="row" style="border: 1px solid #ccc; padding-top: 5px; margin-bottom: 10px;">
   <div class="col-md-3">
       <img src="<?php echo base_url('assets/images/061.jpg'); ?>" alt="MTH-061" />/* This is where I'm stuck*/
   </div>
   
   <div class="col-md-6">
       <h4><?php echo $accessories_item['title']; ?></h4>
       <p><?php echo $accessories_item['description']; ?></p>
       <p><?php echo $accessories_item['subdescription']; ?></p>
       <p><strong><p><?php echo $accessories_item['subnote']; ?></p></strong></p>
   </div>
   
   <div class="col-md-3 text-right">
       <p>£ <?php echo $accessories_item['price']; ?></p>
   </div>

</div>

<?php endforeach; ?>

Any help, guidance or link to a help resource would be greatly appreciated...

My apologies if this has already been covered as it seems so simple!

Thank you in advance,

Best regards,

Alan
Reply

#2
What error are you getting?
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#3
(10-16-2017, 03:04 AM)InsiteFX Wrote: What error are you getting?

Hi InsiteFX - thanks for the speedy reply!!

Dependent on what I've tried I get a range of errors, mostly parsing ones or just nothing on the page.

I've been for a coffee and taken another look and have managed to get them to display now using:

Code:
       <img src="<?php echo base_url(); ?><?php echo $accessories_item['imagesrc']; ?> " alt="<?php echo $accessories_item['imageAlt']; ?>" />

This displays the image and the source code of the rendered page is correct.

This has frustrated me over a couple of days but I want to get to grips with CodeIgniter as I can see massive efficiencies already over the Joomla site it will replace.

However, just because it works (and it "looks right" to me) I am keen to follow the "CodeIgniter way".

Therefore, I'm wondering if this follows convention or there is a better way?

Thanks again,

Alan
Reply

#4
You seem to be doing it correctly, but it could be simplified.

PHP Code:
<img src="<?php echo base_url($accessories_item['imagesrc']); ?>" alt="<?php echo $accessories_item['imageAlt']; ?>" /> 

Although you may have already stored a / at the beginning of the file name in the db that CI might insert, giving you two // after the base url. Not sure if it gets trimmed out automatically or not.

Paul.
Reply

#5
(10-16-2017, 10:34 AM)PaulD Wrote: You seem to be doing it correctly, but it could be simplified.

PHP Code:
<img src="<?php echo base_url($accessories_item['imagesrc']); ?>" alt="<?php echo $accessories_item['imageAlt']; ?>" /> 

Although you may have already stored a / at the beginning of the file name in the db that CI might insert, giving you two // after the base url. Not sure if it gets trimmed out automatically or not.

Paul.

Hi Paul,

Apologies for the delay in my reply!

Thanks for taking a look. I'm all for taking a simplified approach.

I did not have the / in the DB and have left that as is. I've swapped my snippet for your more efficient sample and it works as expected.

Thank you for that - I can press on with my other views now!

Best regards,

Alan
Reply

#6
PHP Code:
/**
 * asset ()
 * ------------------------------------------------------------------------
 *
 * Assumes that all resources are in a asset directory in the root
 * along with index.php. Place in ./application/helpers in a file.
 *
 * USAGE in VIEW:
 *
 * <?php echo asset('path/filename.ext'); ?>
 */
if ( ! function_exists('asset'))
{
    
/**
     * asset ()
     * -------------------------------------------------------------------
     *
     * @param  $uri
     * @return mixed
     */
    
function asset($uri)
    {
        
$CI get_instance();

        return 
$CI->config->base_url('assets/'.$uri);
    }

What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#7
(10-23-2017, 08:45 AM)InsiteFX Wrote:
PHP Code:
/**
 * asset ()
 * ------------------------------------------------------------------------
 *
 * Assumes that all resources are in a asset directory in the root
 * along with index.php. Place in ./application/helpers in a file.
 *
 * USAGE in VIEW:
 *
 * <?php echo asset('path/filename.ext'); ?>
 */
if ( ! function_exists('asset'))
{
    
/**
     * asset ()
     * -------------------------------------------------------------------
     *
     * @param  $uri
     * @return mixed
     */
    
function asset($uri)
    {
        
$CI get_instance();

        return 
$CI->config->base_url('assets/'.$uri);
    }


Hi InsiteFX,

Thanks for this - I think it will be very useful as I have quite a lot of views to do!

Best regards,

Alan
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.