user_777
user_777

Reputation: 805

Results that include space don't get fetched

This is the code where I used to get results of different category names. Here, if the category names have space the result won't get displayed while the names without having spaces will get displayed.

This is my code:

<ul>
    <li><a href="<?php echo base_url();?>clients">View All </a></li>
    <?php foreach($clientdropdown as $row){?>
    <li class="<?php if($active_mn== $row->id) echo 'active'; ?>">
        <a href="<?php echo base_url();?>client/<?php echo $row->category_name;?>">
            <?php echo $row->category_name;?>
            <span></span>
        </a>
    </li>
         <?php }?>

       </ul>

I'm a little bit confused with where to use replace() to avoid spaces.

i had changed my code please see that

Upvotes: 0

Views: 93

Answers (1)

SML
SML

Reputation: 1265

Having looked at what you are trying to do in your edited code, the problem you had isn't that $row->category_name won't fetch.
It is to do with the space in the URL not being probably encoded so the page won't redirect correctly.

URL encoding replaces unsafe ASCII characters with a "%" followed by two hexadecimal digits.
Space is unsafe ASCII character.
URLs cannot contain spaces. URL encoding normally replaces a space with a plus (+) sign or with %20.

urlencode() translates space into +
rawurlencode() translates parameters into hex code, which is %20 for space

<ul>
    <li><a href="<?php echo base_url();?>clients">View All </a></li>
    <?php foreach($clientdropdown as $row){?>
    <li class="<?php if($active_mn== $row->id) echo 'active'; ?>">
        <a href="<?php echo base_url();?>client/<?php echo rawurlencode($row->category_name);?>">
            <?php echo $row->category_name;?>
            <span></span>
        </a>
    </li>
         <?php }?>

       </ul>

UPDATE
codeigniter translates space to underscore when it creates directory with name that contains space, so the correct path to access the directory created using category name "Hello World" should be "Hello_World", for this particular case str_replace(" ", "_", $row->category_name) was all there was needed.

Upvotes: 1

Related Questions