简体   繁体   中英

Get rows from DB with PHP, get the values from rows and insert in HTML

I want to get the rows from the DB that match the criteria, get specific values from each row and paste those values in different HTML.
eg DB matches from criteria = row with id of 3, 7 and 30
I want to take 'name' from row id 3 and put it in <div id='firstResult'></div>
Then take 'name' from row id 7 and put it in <div id='secondResult'></div> Finally, I want to take 'name' from row id 30 and put it in <div id='thridResult'></div>

I know how to put them in HTML, I'm struggling with the PHP part. So far I got:

<?php
    include_once 'databaseconn.php';

    $ramResult = $_POST['ram'];
    $graphcardResult = $_POST['graphcard'];
    $processorResult = $_POST['processor'];
    $sql = "SELECT * FROM laptops WHERE ram = '$ramResult' AND graphcard = '$graphcardResult' AND processor = '$processorResult' ;"; 
    $result = mysqli_query($conn, $sql);  
    if ($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
            $nameResult = $row['name'];
            $ramResult = $row['ram'];
            $graphcardResult = $row['graphcard']; 
            $processorResult = $row['processor']; 
            $imgResult = $row['img'];  
            $imgId = $row['id'];
        }
    }
?>

I thought of giving the results numbers like first result is number 1, second is 2, etc. and then getting $firstResult['name'] Not sure if this is the right approach and since I'm a newb I'm having a hard time figuring out how to do this. I hope you can push me in the right direction.

for completion, DB:

id | name | ram | graph | processor | url
1  | data | data| data  | data      | data
2  | data | data| data  | data      | data
3  | data | data| data  | data      | data

etc..

Conceptually speaking, you can achieve this a number of ways.

    <?php
    include_once 'databaseconn.php';

    $ramResult = $_POST['ram'];
    $graphcardResult = $_POST['graphcard'];
    $processorResult = $_POST['processor'];
    $sql = "SELECT * FROM laptops WHERE ram = '$ramResult' AND graphcard = '$graphcardResult' AND processor = '$processorResult' ;"; 
    $result = mysqli_query($conn, $sql);
    $list = mysqli_fetch_all ($result, MYSQLI_NUM); //RETURN INDEXED ARRAY
    //CREATES AN EMPTY STRING
    $output = "";
    if(count($list) > 0){ //CHECK IF THERE IS A RESULT.
    //if ($result->num_rows > 0) {
        for($i=0; $i < count($list); $i++){
        //while($row = $result->fetch_assoc()) {
            $nameResult = $list[$i]['name'];
            $ramResult = $list[$i]['ram'];
            $graphcardResult = $list[$i]['graphcard']; 
            $processorResult = $list[$i]['processor']; 
            $imgResult = $list[$i]['img'];  
            $imgId = $list[$i]['id'];
            //CONDITIONALLY CHECK VALUE AND ALTER OUTPUT
            if($i == "0"){
              // .= operator will append the string to the end of the existing string. 
              $output .= "<div id='firstResult'>".$nameResult."</div>";
            }elseif($i == "1"){
              $output .= "<div id='secondResult'>".$nameResult."</div>";
            }elseif($i == "2"){
              $output .= "<div id='thirdResult'>".$nameResult."</div>";
            }else{
              /*... default output ... */
            }
        }
      //ONCE OUTPUT IS BUILT, You can echo it, or use it in your template. 
      echo $output;

    }
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM