1

I'm using CodeIgniter and MySQL as db. I know it can be solved with MySQL query, but I wonder if it can be solved with a CodeIgniter query, otherwise normal PHP MySQL query can work also.

I have Table with 3 columns, it has many fields but I have shown here 4 entries, I didn't want to make a table with many columns but only 1 row of data. so instead I went for this style of table as someone suggested on this website.

Problem is I never worked with this kind of table.

       SettingsID           SettingsKey        SettingsValue
----------------------------------------------------------------------
          1            |     facebookLink     | facebook.com
          2            |     twitterLink      | twitter.com
          3            |     youtubeLink      | youtube.com
          4            |     googlePlusLink   | googleplus.com

Now I want to run a query that should return me rows in to columns. I searched over net and found some solutions but I am not good with these new queries.

I suppose this guy had same kind of problem like I have but in his case he has same value repeated in column, where my SettingsKey has all the values unique. Here is the link to similar kind of question :

mysql select dynamic row values as column names, another column as value

I can't understand his query and that query I am not sure if is any use of me.

How can I build a query to return row values of SettingsKey as columns?

3
  • So do you want to get the SettingsValue of each SettingsKey? Commented Aug 28, 2014 at 12:17
  • @Bluedayz Yes so that i can show the data in to the form.. Commented Aug 28, 2014 at 12:18
  • @SizzlingCode what is your expected output?? Commented Aug 28, 2014 at 12:18

2 Answers 2

1

This should work:

<?php
$db = new PDO('mysql:host=localhost;dbname=DBNAME;charset=utf8', USERNAME, PASSWORD);
$stmt = $db->query("SELECT SettingsKey, SettingsValue FROM Settings");
$links = array();
while($row = $stmt->fetch()) {
    $links[$row['SettingsKey']] = $row['SettingsValue'];
}
$db = null;

This code queries this table and for each row the SettingsKey and the SettingsValue will be shown.

Now you can get the facebookLink like this:

echo $links['facebookLink'];

Hope this helps.

Sign up to request clarification or add additional context in comments.

5 Comments

yes. i could use that query easily in codeigniter, but i dont know how to explain this. What if i want the value of facebookLink in SettingsKey and echo it in form. like echo $facebookLink; how can i achieve that in your query?
Yes. i just finished with setting it up in my project, worked like a charm. Many Thanks... However i am not using PDO, so did some changes.
So do you use mysqli? That's your choice of course. I used mysqli for a long time, but I fell in love with PDO :)
No i am using CodeIgniter with Smarty Templates, Codeigniter is a PHP framework. All i needed was help in database, i am not good in databases. {{foreach $data as $key =>$row}} {{$sKey[$row->settingsKey] = $row->settingsValue}} {{/foreach}}
No i am using CodeIgniter with Smarty Templates, Codeigniter is a PHP framework. All i needed was help in database, i am not good in databases. {{foreach $data as $key =>$row}} {{$sKey[$row->settingsKey] = $row->settingsValue}} {{/foreach}}
0
$sql = "
        SELECT SettingsKey, SettingsValue
        FROM Settings
       ";

$q = $this->db->query($sql);
return $q->result_array();

Go to your controller

$data['settings'] = $this->model_selection->your_function();
$this->load->view('example', $data);

And lasty on your view

<?php if(!empty($settings)): ?>

       <?php foreach($settings as $k => $v): ?>

        //print your staff in here mix with html and go crazy
       <?php endforeach; ?>

<?php endif ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.