CodeIgniter Forums
Make link generator - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10)
+--- Thread: Make link generator (/showthread.php?tid=80683)



Make link generator - zhikri - 12-01-2021

Hi,
I want to make link generator then i'll use it for generate QR-Code image.
i've created it already but i think it's not a good way to generate it
Code:
public function linkGenerate()
    {
        $id = $this->confirmModel->select('TRACKID')->findAll();
        $url = [];
            foreach ($id as $ids) {
                $link['id'] = base_url($ids);
                $url [] = $link;
            }
        return $url;
    }

when i dump the url field, it will show array like:
https://localhost:8080/index.php/1
https://localhost:8080/index.php/2
and so on

i getting struggle when i want to add any segment before track id like https://localhost:8080/index.php/user/1

any suggestion the better way to generate this url? 

thanyou



RE: Make link generator - kenjis - 12-01-2021

PHP Code:
$link['id'] = base_url('user/'.$ids); 



RE: Make link generator - zhikri - 12-01-2021

(12-01-2021, 05:12 PM)kenjis Wrote:
PHP Code:
$link['id'] = base_url('user/'.$ids); 

it's show array of string exception


RE: Make link generator - kenjis - 12-01-2021

Do you mean "Array to string conversion"?
If so, just to add code to process to get string from an array.


RE: Make link generator - zhikri - 12-01-2021

(12-01-2021, 07:36 PM)kenjis Wrote: Do you mean "Array to string conversion"?
If so, just to add code to process to get string from an array.

yes array to string conversation, which array I have to get it string? url or id?
i have no idea


RE: Make link generator - kenjis - 12-01-2021

You can know the type of a variable with dd($variable).


RE: Make link generator - zhikri - 12-01-2021

(12-01-2021, 08:00 PM)kenjis Wrote: You can know the type of a variable with dd($variable).

still not solved, i have dd() it but i have no idea how to solve this array to string conversion


RE: Make link generator - davis.lasis - 12-02-2021

PHP Code:
Maybe this will help you?

# CONTROLLER
public function linkGenerate()
{
    $urls = [];
    
    $results 
$this->confirmModel->select('TRACKID')->findAll();
    
    
foreach ($results as $result) {
         $urls[$result] = base_url('user/'.$result);
    }

    return $urls;
}


// VIEW
dd($urls);

Will give you array structure like this

array (
 
'TRACKID1' => 'https://localhost:8080/index.php/user/TRACKID1',
 
'TRACKID2' => 'https://localhost:8080/index.php/user/TRACKID2',
 
'TRACKID3' => 'https://localhost:8080/index.php/user/TRACKID3',
 
'TRACKID4' => 'https://localhost:8080/index.php/user/TRACKID4',
 
'TRACKID5' => 'https://localhost:8080/index.php/user/TRACKID5',
)


# in frontend you can output data like:

foreach ($urls as $track_id => $url) {
    echo 'Track ID: '.track_id.' with URL: '.$url;




RE: Make link generator - InsiteFX - 02-24-2022

PHP Code:
// change this
echo 'Track ID: '.track_id.' with URL: '.$url;

// to this
echo 'Track ID: '.$track_id.' with URL: '.$url