0

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

4
  • be creative or just look for reference for fancy css table styling by searching in google Commented Apr 14, 2015 at 8:26
  • im not asking about the css here.. I want to display the results with php.. Its the php im asking for. let me refrase my question Commented Apr 14, 2015 at 8:27
  • why not use while? odd, then foreach Commented Apr 14, 2015 at 8:32
  • An alternative approach to the options below of looping the fetch, you could use fetchAll and utilize foreach Commented Apr 14, 2015 at 9:17

2 Answers 2

1

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 -->
Sign up to request clarification or add additional context in comments.

Comments

0

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.

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.