0

I have database table with fields id, text and type. And in type I can have values 1 and 2. I want to display one html output if returned type is 1, and other output if type is 2.

table:

id  text       type
1   Some text  1
2   Blah Blah  2

So my simple query SELECT * from table will return array of values. In my HTML I want to output it like this:

<?php foreach ($model as $data): ?>

<h4>Text where status is 1:</h4>  

echo $data['text'];

<h4>Text where status is 2:</h4>

echo $data['text'];

<?php endforeach ?>

I have tried with using if inside foreach like this:

<?php if ($data['type'] == "1"): ?>
    <h4>Text where status is 1:</h4>

    echo $data['text'];
<?php else: ?>
    <h4>Text where status is 2:</h4>

    echo $data['text'];
<?php endif ?>

But this doesn't really work. Is my solution for this to have one query that will return text where type is 1, and another where type is 2 ? Or there is some better solution for this ?

I would like output like this:

<h4> Text where status is 1 </h4>    


First text.

Second text.

Third text.


<h4> Text where status is 2 </h4>  


Fourth Text.

Fifth Text.
4
  • is $model the variable containing the SQL query result? Commented Mar 9, 2015 at 14:19
  • Yes it contains query result Commented Mar 9, 2015 at 14:21
  • And what is $offer? I mean, what is the condition you need? if $offer is "1" then print the text with status 1 AND $data['text']? Commented Mar 9, 2015 at 14:23
  • Sorry, $offer is $data, I forgot to replace name when posted here. Commented Mar 9, 2015 at 14:37

2 Answers 2

2

In such a case, I would go in this way, sir:

$outputs = array("1" => array(),"2" => array());

foreach ($model as $data) {
   $outputs[$data['type']][] = $data['text'];
}

foreach ($outputs as $type => $values) {
   echo "<h4>Text where status is {$type}: </h4><br />";
   foreach ($values as $text) {
      echo $text . " <br />";
   }
}

Explanation:

We can't, in PHP, print everything inline in such a way, that's why we are:

  1. Declaring an array with two values, which are they "$data['type']" that needs to be checked. By doing that, we are declaring the types as key and an array corresponding to each key.
  2. While we loop through the results, we push the result inside the correct array of the corresponding key above. passing $output[$data['type']] on a result that has 'type' 2 is the exact same as doing: $output['2']. the next [] means "push that element inside that array", therefore we're literally populating the array according to its key.
  3. Because of the structure of the array by looping it again, we loop it by getting the TYPE ("key") and it's value, which is $data['text'] and we print first the type (dinamically, so that if tomorrow you want to add a type 3 you can do that without many issues) and then the values corresponding to its type.

The output:

<h4>Text where status is 1: </h4><br />hello <br />ohai dude <br /><h4>Text where status is 2: </h4><br />hello dude <br />

From an input array like this:

$model = array(
0 => array( "id" => "0",
            "type" => "1",
            "text" => "hello"
            ),
1 => array( "id" => "1",
            "type" => "2",
            "text" => "hello dude"
            ),
2 => array( "id" => "2",
            "type" => "1",
            "text" => "ohai dude"
            )
);
Sign up to request clarification or add additional context in comments.

11 Comments

This will output "Text where status is 2" with every row returned where status is 2. I need to output it on top, and then all rows with type of 2 under it.
On top? what do you mean by "on top"?
Ooooh okay I got it. Man, you could've been way clearer! Stay tuned.
I have edited my question by providing example of desired output.
I have edited my question now, it should be more clear.
|
0
// connect to db first
$link = mysqli_connect('host','user','pass','database');
// get something from db
$result = mysqli_query($link, "SELECT * FROM table ORDER BY type");

// loop through result and echo
$flag1 = false;
$flag2 = false;
while($data = mysqli_featch_array($result, MYSQLI_ASSOC)) {
   if($data['type'] == "1") {
      if(!$flag1) {
         echo '<h4>Text where status is 1:</h4>';
         $flag = true;
      }
      echo $data['text'];
   } else {
      if(!$flag2) {
         echo '<h4>Text where status is 2:</h4>';
         $flag = true;
      }
      echo $data['text'];
   }   
}

3 Comments

I need to have these headlines : <h4>Text where status is 1:</h4>. This one is for all text where status is 1, and then comes : <h4>Text where status is 2:</h4> with text with status of 2
Edited query so result is ordered by type. Added flag variables to output only one title.
@black-room-boy then instead of echoing in the loop, save to an array and then print whatever you want.

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.