Mensur
Mensur

Reputation: 475

How to display mysql array with limit together with PHP

I have a mysql query together with my html code looking like this.

                <?php 

                    /////////////////////////////////////////////////////////////
                    // RANDOM USER DISPLAYED WHICH MATCHS
                    /////////////////////////////////////////////////////////////

                    $statement = $dbConn->prepare("SELECT user_name, profile_image_url, user_age, country FROM users WHERE profile_image = '2' AND gender = ? ORDER BY RAND() LIMIT 2;");
                    $statement->execute(array($logged_in_user['searching_for']));
                    $random_match= $statement->fetch(PDO::FETCH_BOTH);

                    /////////////////////////////////////////////////////////////
                    // END OF RANDOM QUESTIONS
                    /////////////////////////////////////////////////////////////

                    ?>


                <div id="front_match">
                    <div class="front_box">
                        <div class="front_box_left">
                            <div style="position: relative;">
                                    <img src="images/woman_1.jpg" style="max-width:100%;height:auto;">
                                    <div class="transparent">Information about user one comes here</div>
                            </div>
                        </div>
                        <div class="front_box_right">
                            <div style="position: relative;">
                                    <img src="images/woman_2.jpg" style="max-width:100%;height:auto;">
                                    <div class="transparent">Information about user two comes here</div>
                            </div>
                        </div>
                    </div> <!-- div box finished here -->

                </div> <!-- div match finished here -->

How to i insert information received from the database in "Information about user one" and then "Information about user two"? You can see that i'm using limit 2 so i want to display 1 results in one place and second result in second place

Cheerz

Upvotes: 0

Views: 73

Answers (2)

Logan Wayne
Logan Wayne

Reputation: 5991

You can still use fetch_array to achieve your goal. Store it in an array, then you can call it whenever you want.

while($statement->fetch()){

  $user_name_arr[] = $user_name;
  $user_age_arr[] = $user_age;
  /* ... REST OF CODE ... */

}

Then call them manually:

<div id="front_match">
  <div class="front_box">
    <div class="front_box_left">
      <div style="position: relative;">
        <img src="images/woman_1.jpg" style="max-width:100%;height:auto;">
        <div class="transparent">
          <?php
            echo "Name: ".$user_name_arr[0];
            echo "Age: ".$user_age_arr[0];
            /* ECHO REST OF THE INFORMATION */
          ?>
        </div>
      </div>
    </div>
    <div class="front_box_right">
      <div style="position: relative;">
        <img src="images/woman_2.jpg" style="max-width:100%;height:auto;">
        <div class="transparent">
          <?php
            echo "Name: ".$user_name_arr[1];
            echo "Age: ".$user_age_arr[1];
            /* ECHO REST OF THE INFORMATION */
          ?>
        </div>
      </div>
    </div>
  </div> <!-- div box finished here -->
</div> <!-- div match finished here -->

Just wondering:

I'm just wondering, why do you have to separate them and not use while loop. And just go with this:

<div id="front_match">
  <div class="front_box">
    <?php

      while($statement->fetch()){
        ?>
        <div class="front_box_left">
          <div style="position: relative;">
            <img src="<?php echo $profile_image_url; ?>" style="max-width:100%;height:auto;">
            <div class="transparent">
              <?php
                echo "Name: ".$user_name;
                echo "Age: ".$user_age;
                /* ECHO REST OF THE INFORMATION */
              ?>
            </div>
          </div>
        </div>
        <?php
      } /* END OF WHILE LOOP */

    ?>
  </div> <!-- div box finished here -->
</div> <!-- div match finished here -->

Upvotes: 1

DocRattie
DocRattie

Reputation: 1423

You want excatly two rows? Then fatch two times:

$first_match = $statement->fetch(PDO::FETCH_BOTH);
$second_match = $statement->fetch(PDO::FETCH_BOTH);

You can use $first_match for the first table and $secon_match for the second table.

Upvotes: 0

Related Questions