0

I need to insert specific array values in a HTML form. I query, fetch and array from a mysql table. When I print_r($array) I get the below output.

Question: Let´s say I want to echo out the value Dæk Bag to a html form question, how do I do that?

INFO: I have 40 questions in the form.

Ps. The app is for own use only, and I am aware of the use of mySQLi or PDO instead of mySQL -> It will be change asap.

Array:

Array
(
    [0] => 1
    [id] => 1
    [1] => Dæk For
    [title_task_DK] => Dæk For
)
Array
(
    [0] => 2
    [id] => 2
    [1] => Dæk Bag
    [title_task_DK] => Dæk Bag
)
Array
(
    [0] => 3
    [id] => 3
    [1] => Dæk Alm.
    [title_task_DK] => Dæk Alm.
)
Array
(
    [0] => 4
    [id] => 4
    [1] => Dæk Indl.
    [title_task_DK] => Dæk Indl.
)
Array
(
    [0] => 5
    [id] => 5
    [1] => Slange/Lapning For
    [title_task_DK] => Slange/Lapning For
)

My PHP code:

<?php
$results = mysql_query("SELECT * FROM task_list");
    while($array = mysql_fetch_array($results)) {    
        print_r($array);
    }
?>

My mySQL table:

CREATE TABLE `task_list` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title_task_DK` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=37 DEFAULT CHARSET=utf8;
Nomis
  • 437
  • 1
  • 4
  • 13
  • You have to query it directly via MySQL - why else would you like to use a database for, then? : )) – moonwave99 Nov 19 '14 at 12:47
  • ... The array is queried from a table in my DB. I do not fully understand what you mean @moonwave99. Please explain more detailed or use an ex. Thank you. – Nomis Nov 19 '14 at 13:20
  • Use `WHERE` in your query clause, as other user suggested. – moonwave99 Nov 19 '14 at 14:10
  • ... @moonwave99 I have 40 questions in the form so a mysql_querying with WHERE on every single question seems like a lot of unnecessary code. There has to be an easier way to solve this or? – Nomis Nov 19 '14 at 14:13
  • Well in case you have all the data in one array, just use `array_filter` to get what you need [there are many examples online[. – moonwave99 Nov 19 '14 at 18:31
  • .. @moonwave99 my array looks like the ex. in my question. Please provide an ex. on how you would do an array_filter on my array. Thank you. – Nomis Nov 19 '14 at 22:42
  • I have already given by showing you the `array_filter` function - the rest is on you, this is no "give me the code" website sorry. – moonwave99 Nov 20 '14 at 10:56
  • To whomever gave this question a -1, please explain why it deserved a downgrade? – Nomis Nov 20 '14 at 13:22

4 Answers4

0

You need to do the following.

    <?php
    $requiredName = 'Dæk Bag';
    $results = mysql_query("SELECT * FROM task_list");
        while($array = mysql_fetch_array($results)) {
           if ($array['title_task_DK'] == 'Dæk Bag') {
            print($array['title_task_DK']);//Use this for displaying purpose.
           }
        }
    ?>

Explanation: You need only one element from the row (not whole array).

Pupil
  • 23,834
  • 6
  • 44
  • 66
  • If I test your suggestion @Programming-Student , `print_r($array['title_task_DK']);` I get all title_task_DK from all the arrays. I only want one, in this ex. the value `Dæk Bag` Please see my **My mySQL table:** in the question. – Nomis Nov 19 '14 at 12:41
  • ... I can not make your code work @Programming-student. I see what you want to do, but I have 40 questions in the form so using your suggestion on every single question would be a lot of unnecessary code. There has to be an easier way to solve this. – Nomis Nov 19 '14 at 13:16
0

try to match a key/value to get expected result from array

while($array = mysql_fetch_array($results)) {   
    if($array['id'] == 2) {
      echo $array['title_task_DK'];
    }
 }
Rakesh Sharma
  • 13,680
  • 5
  • 37
  • 44
  • ...I see what you want to do @Rakesh-Sharma, but as I comment on another answer from "programming-Student", I have 40 questions in the form, so using this approach on every single question seems like a lot of unnecessary code. There has to be an easier way to solve this. – Nomis Nov 19 '14 at 13:34
0

You can do it with MySQL itself.

$results = mysql_query("SELECT * FROM task_list WHERE title_task_DK => 'Dæk Bag'");

Additionally, if you want only one row, put limit.

$results = mysql_query("SELECT * FROM task_list WHERE title_task_DK => 'Dæk Bag' LIMIT 1");

Existing PHP code will work fine.

Pupil
  • 23,834
  • 6
  • 44
  • 66
  • As i comment on you previously answer @programming-student, I have 40 questions in the form so using this suggestion on every single question seems like a lot of unnecessary code. There has to be an easier way to solve this! – Nomis Nov 19 '14 at 13:39
0

The below PHP code solved my question and echos out the wanted value Dæk Bag I needed in my HTML form.

<?php
 $results = mysql_query("SELECT * FROM task_list");
 $value = array();
 while($array = mysql_fetch_array($results)) {    
  $value[] = $array['title_task_DK'];
}
  echo($value[1]);
?>

Do you need a foreach loop in the above code: I found this Stack Overflow link helpful.

Thank you all for commenting and answering.

Ps. that said, please read: Why shouldn't I use mysql_* functions in PHP?.

Community
  • 1
  • 1
Nomis
  • 437
  • 1
  • 4
  • 13