CodeIgniter Forums

Full Version: Looping file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hello,

Can anyone help me printing all the files that I have in uploads/ ? to the screen.  Each pictures must be printed in each row.

views/slideshows.php


PHP Code:
<table style="padding: 10px;">
        <tr>
        <td><b>EDIT CAPTION</b></td>
        <td><center><b>IMAGES</b></td>
        <td><b>DELETE</b></center></td>
        </tr>
        <tr>
        <td><input type="text" name="caption" value=""></td>
        <td><img src="<?php echo base_url('uploads/pic1.jpg'); ?>" height="300" width="400"></td>
        <td><button class="delete">DELETE</button></td>
        </tr>
        <tr>
        <td><input type="text" name="caption" value=""></td>
        <td><img src="<?php echo base_url('uploads/pic2.jpg'); ?>" height="300" width="400"></td>
        <td><button class="delete">DELETE</button></td>
        </tr>
        </table


How?
Maybe a foreach loop
PHP Code:
<?= form_open(); ?>
    <table>
        <thead>
            <tr>
                <th>Edit Caption</th>
                <th>Images</th>
                <th>Delete</th>
            </tr>
        </thead>
        <tfoot>
            <tr>
                <td colspan='3'>
                    <input type="submit" name="delete" class='btn-delete' value="Delete Selected" />
                    <input type="submit" name="update" class='btn-update' value="Update Captions" />
                </td>
            </tr>
        </tfoot>
        <tbody>
            <?php foreach $images as $image) : ?>
            <tr>
                <td><input type="text" name="caption[]" value="<?= $image['caption']; ?>" /></td>
                <td><img src="<?= base_url($image['src']; ?>" height="<?= $image['height']; ?>" width="<?= $image['width']; ?>" /></td>
                <td><input type="checkbox" class='delete' name='checked[]' value="<?= $image['id']; ?>" /></td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
<?= form_close(); ?>
Hello,

I try this:  <?= base_url($image['src']; ?>


PHP Code:
<br><br>
<
table style="padding: 10px;">
<
tr>
<
td><b>EDIT CAPTION</b></td>
<
td><center><b>IMAGES</b></td>
<
td><b>DELETE</b></center></td>
</
tr>
<
tr>
<
td><input type="text" name="caption" value=""></td>
<
td><img src="<?php echo base_url('uploads/'.<?= base_url($image['src']; ?>'); ?>" height="300" width="400"></td>
<
td><button class="delete">DELETE</button></td>
</
tr>
<
tr>
<
td><input type="text" name="caption" value=""></td>
<
td><img src="<?php echo base_url('uploads/pic2.jpg'); ?>" height="300" width="400"></td>
<
td><button class="delete">DELETE</button></td>
</
tr>
       </table>
</
div>

       <?php echo form_close(); ?>
       <?php if (isset($success_msg)) { echo $success_msg; } ?>
       </div> 

And it does not read my codes.

Parse error: syntax error, unexpected '<' in C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\CompanyGiondaCI\application\views\addslideshows.php on line 112
A PHP Error was encountered
Severity: Parsing Error
Message: syntax error, unexpected '<'
Filename: views/addslideshows.php
Line Number: 112
Backtrace:


I revise it into this:   

Line 112:  is <td><img src="<?php echo base_url('uploads/'.<?= base_url($image['src']; ?>); ?>" height="300" width="400"></td>



PHP Code:
<br><br>
        <table style="padding: 10px;">
        <tr>
        <td><b>EDIT CAPTION</b></td>
        <td><center><b>IMAGES</b></td>
        <td><b>DELETE</b></center></td>
        </tr>
        <tr>
        <td><input type="text" name="caption" value=""></td>
        <td><img src="<?php echo base_url('uploads/'.<?= base_url($image['src']; ?>); ?>" height="300" width="400"></td>
        <td><button class="delete">DELETE</button></td>
        </tr>
        <tr>
        <td><input type="text" name="caption" value=""></td>
        <td><img src="<?php echo base_url('uploads/pic2.jpg'); ?>" height="300" width="400"></td>
        <td><button class="delete">DELETE</button></td>
        </tr>
        </table>
        </div>
        
        
<?php echo form_close(); ?>
        <?php if (isset($success_msg)) { echo $success_msg; } ?>
        </div>
  
This will never work.

You can't start php code without closing the previous php code block.
Another mistake is to include base_url() twice in one image source path.
I think your code should be:

PHP Code:
<td><img src="<?= base_url() . 'uploads/' . $image['src'];?>" height="300" width="400" /></td
(07-20-2016, 08:35 AM)davy_yg Wrote: [ -> ]Hello,

I try this:  <?= base_url($image['src']; ?>


PHP Code:
<br><br>
<
table style="padding: 10px;">
<
tr>
<
td><b>EDIT CAPTION</b></td>
<
td><center><b>IMAGES</b></td>
<
td><b>DELETE</b></center></td>
</
tr>
<
tr>
<
td><input type="text" name="caption" value=""></td>
<
td><img src="<?php echo base_url('uploads/'.<?= base_url($image['src']; ?>'); ?>" height="300" width="400"></td>
<
td><button class="delete">DELETE</button></td>
</
tr>
<
tr>
<
td><input type="text" name="caption" value=""></td>
<
td><img src="<?php echo base_url('uploads/pic2.jpg'); ?>" height="300" width="400"></td>
<
td><button class="delete">DELETE</button></td>
</
tr>
       </table>
</
div>

       <?php echo form_close(); ?>
       <?php if (isset($success_msg)) { echo $success_msg; } ?>
       </div> 

And it does not read my codes.

Parse error: syntax error, unexpected '<' in C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\CompanyGiondaCI\application\views\addslideshows.php on line 112
A PHP Error was encountered
Severity: Parsing Error
Message: syntax error, unexpected '<'
Filename: views/addslideshows.php
Line Number: 112
Backtrace:

That's because you copied part of one line of my code into the wrong place within your existing code. 

PHP Code:
<?= 'something'?>

is a short-hand for 

PHP Code:
<?php echo 'something'?>

and it is an error to put <?= inside a PHP tag.

The basic idea is that your controller would pass an array called $images to the view which contains a series of arrays, each of which contains the definition of the various properties of your image (caption, src, width, height). 

PHP Code:
$images = [
    [
        'caption' => '',
        'src' => 'uploads/pic1.jpg',
        'height' => 300,
        'width' => 400,
        'id' => 1,
    ],
    [
        'caption' => '',
        'src' => 'uploads/pic2.jpg',
        'height' => 300,
        'width' => 400,
        'id' => 2,
    ],
];

$this->load->view('add slideshows', ['images' => $images]); 

Then you could loop through $images to extract the properties of each $image into a row in your table. So, $image['src'] would return 'uploads/pic1.jpg' for your first image, 'uploads/pic2.jpg' for your second image, etc..
This is what I try to print all gallery in rows.  Can somebody help me fix this error message?  Thanks in advance.

Parse error: syntax error, unexpected ';' in C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\CompanyGiondaCI\application\views\addslideshows.php on line 114
A PHP Error was encountered
Severity: Parsing Error
Message: syntax error, unexpected ';'
Filename: views/addslideshows.php
Line Number: 114
Backtrace:


Line 114:  <td><img src="<?php echo base_url('uploads/'.base_url($images_item['src']; )); ?>" height="300" width="400"></td>

       
views/addslideshows.php


PHP Code:
<?php foreach ($images as $images_item): ?>
        
        <br><br>
        <table style="padding: 10px;">
        <tr>
        <td><b>EDIT CAPTION</b></td>
        <td><center><b>IMAGES</b></td>
        <td><b>DELETE</b></center></td>
        </tr>
        <tr>
        <td><input type="text" name="caption" value=""></td>
        <td><img src="<?php echo base_url('uploads/'.base_url($images_item['src'];)); ?>" height="300" width="400"></td>
        <td><button class="delete">DELETE</button></td>
        </tr>
        <tr>
        <td><input type="text" name="caption" value=""></td>
        <td><img src="<?php echo base_url('uploads/pic2.jpg'); ?>" height="300" width="400"></td>
        <td><button class="delete">DELETE</button></td>
        </tr>
        </table>
        </div>
        
        <?php echo form_close(); ?>
        <?php if (isset($success_msg)) { echo $success_msg; } ?>
        </div>
  
        <?php endforeach; ?>


cpages/addslideshows.php

PHP Code:
public function addslideshows() {

        
$this->load->model('Mpages');
        
        
$data['images'] = $this->Mpages->call_gallery();
        
        
$this->load->view('addslideshows'$data); 
    
    } 
Seriously, you cannot see what might be wrong with this?

PHP Code:
<?php echo base_url('uploads/'.base_url($images_item['src']; )); ?>

And MWhitney already took the time to refer to this exact problem, reread his post or reread the documents for base_url.
Might as well talk to a wall!
(07-20-2016, 09:37 PM)davy_yg Wrote: [ -> ]This is what I try to print all gallery in rows.  Can somebody help me fix this error message?  Thanks in advance.

Parse error: syntax error, unexpected ';' in C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\CompanyGiondaCI\application\views\addslideshows.php on line 114
A PHP Error was encountered
Severity: Parsing Error
Message: syntax error, unexpected ';'
Filename: views/addslideshows.php
Line Number: 114
Backtrace:


Line 114:  <td><img src="<?php echo base_url('uploads/'.base_url($images_item['src']; )); ?>" height="300" width="400"></td>

Despite my better judgment, I'm going to attempt to help you here.

A semicolon (Wink indicates the end of a command in programming languages like PHP, C, JavaScript, etc. If you end a command with a semicolon before you include all of the necessary components of that command, you will get the message "syntax error, unexpected ';'". 

In this case, you're getting the error because you've called base_url() twice, but haven't closed the parentheses for either call before terminating the command.

It would appear that you need to find some PHP tutorials in addition to some CodeIgniter tutorials, and pay a little more attention to the code you're writing as well as the error messages you're receiving. You might even want to spring for a book if you're going to be doing this a while.
Pages: 1 2