0

I'm new to this, trying to create a website and learning step by step

Here's what I'm trying to do:

  1. User clicks on options which the website creates a result from it and stores it in a variable(e.g. result = laptop with 2GB ram, 2GB graphic card, i5 processor)

  2. I've got a database with 900+ rows where each row contains the laptop name, url, img url, specs, etc. (a lot of data)

  3. After processing of the website it will return the following in the javaScript/ Jquery script:

    let ram = '2GB';
    let graphCard = '2GB';
    let processor = 'i5'
    
  4. Go to the database, lookup all of the rows that equal the criteria and store the values of the rows into profiles e.g. (I guess i would need to store them in JSON?):

    laptop1 {
        ram = "2GB",
        processor = "i5",
        graphCard = "2GB",
        url = "www.blabla1.com"
    }
    laptop2 {
        ram = "2GB",
        processor = "i5",
        graphCard = "2GB",
        url = "www.blabla2.com"
    }
    
  5. From there, get those values will get shown in the result page which Jquery/ JavaScript will insert e.g.:

     <p id=result1></p>
     <p id=result2></p>
     <p id=result2></p>
    

Since I'm a newb, I'm having a hard time setting up step 4 (connecting to the database, get all the data while not loading ALL of the data into the webpage, select some data based on the criteria and inserting the results (laptops and the specs) into the site)

So far I got this:

index.php:

<?php 
    include 'dbh.php';
?>

<!DOCTYPE html>
<html>
<head>
    <title></title>

    <script >GET JQUERY AJAX SRC etc etc</script>

    <script>
        $(document).ready(function() {
            $('#btn').click(function() {
                $('#finalResult').load(data);
            });
        })
    </script>

</head>
<body>

<div id="finalResult">
    <?php

        $sql = 'SELECT * FROM laptops'
        $result = mysqli_query($conn, $sql);    
        }    
    ?>
</div>
<button id="btn">Load Result</button>

</body>
</html>

dbh.php:

<?php

$servername = 'localhost';
$username = 'root';
$password = '';
$dbname = 'laptopInfo';

$conn = mysqli_connect($servername, $username, $password, $dbname);
?>

SQL columns would be:

Laptop_id, laptopUrl, laptopImgUrl, laptopRam, LaptopProccesor, etc...

So far I'm stuck here.. I'm also not sure if I should return the data (from the database) into a JSON file or process it directly.

Hope you guys can help to get to the next step!

EDIT: so here's the HTML part for the buttons:

    <div id="base">
      <button class='button' data-next="games" data-parent="base">Games</button>
      <button class='button' data-next="work" data-parent="base">Work</button>
    </div>
<!----------------------------------------- GAMES ----------------------------------------------->
    <div id="games" class="hidden">
      <button class='button' data-next="games-heavy" data-parent="games">Heavy Games</button>
      <button class='button' data-next="games-light" data-parent="games">Light Games</button>
    </div>

    <div id="games-heavy" class="hidden">
      <button class='button' data-next="games-heavy1" data-parent="games-heavy" final-answer="games-heavy1">First Heavy Game</button>
      <button class='button' data-next="games-heavy2" data-parent="games-heavy" final-answer="games-light1">Second Heavy Game</button>
      <button class='button' data-next="games-heavy3" data-parent="games-heavy" final-answer="games-heavy1">Last Heavy Game</button>
    </div>

    <div id="games-light" class="hidden">
      <button class='button' data-next="games-light1" data-parent="games-light" final-answer="games-light1">First light Game</button>
      <button class='button' data-next="games-light2" data-parent="games-light" final-answer="games-light2">Second light Game</button>
      <button class='button' data-next="games-light3" data-parent="games-light" final-answer="games-light3">Last light Game</button>
    </div>

    <!--------------- end result is a <p> with a button that redirects to the next page (result page). Bellow are two examples -->
    <div id="games-heavy1" class="hidden">
        <p>You have chosen First Heavy GAME.</p>
        <button onclick="nextPage(this);" class="firstHeavyGame button">Klik hier voor de laptops</button> 
    </div>
    <div id="games-heavy2" class="hidden">
        <p>You have chosen Second Heavy GAME.</p>
        <button onclick="nextPage(this);" class="secondHeavyGame button">Klik hier voor de laptops</button>
    </div>

    <!----------------- these <p> will also have buttons that redirect to the next page (result page) -->
    <p id="games-heavy3" class="hidden">You have chosen Last Heavy GAME.</p>
    <p id="games-light1" class="hidden">You have chosen First light GAME.</p>
    <p id="games-light2" class="hidden">You have chosen Second light GAME.</p>
    <p id="games-light3" class="hidden">You have chosen Last light GAME.</p>

<!----------------------------------------- WORK ----------------------------------------------->
    <div id="work" class="hidden">
      <button class='button' data-next="work-heavy" data-parent="work">Heavy Apps</button>
      <button class='button' data-next="work-light" data-parent="work">Light Apps</button>
    </div>

    <div id="work-heavy" class="hidden">
      <button class='button' data-next="work-heavy1" data-parent="work-heavy" final-answer="work-heavy1">First Heavy App</button>
      <button class='button' data-next="work-heavy2" data-parent="work-heavy" final-answer="work-heavy2">Second Heavy App</button>
      <button class='button' data-next="work-heavy3" data-parent="work-heavy" final-answer="work-heavy3">Last Heavy App</button>
    </div>

    <div id="work-light" class="hidden">
      <button class='button' data-next="work-light1" data-parent="work-light" final-answer="work-light1">First Light App</button>
      <button class='button' data-next="work-light2" data-parent="work-light" final-answer="work-light2">Second Light App</button>
      <button class='button' data-next="work-light3" data-parent="work-light" final-answer="work-light3">Last Light App</button>
    </div>

    <!-------------- these <p> will also have buttons that redirect to the next page (result page) -->
    <p id="work-light1" class="hidden">You have chosen First Heavy APP.</p>
    <p id="work-light2" class="hidden">You have chosen Second Heavy APP.</p>
    <p id="work-light3" class="hidden">You have chosen Last Heavy APP.</p>
    <p id="work-heavy1" class="hidden">You have chosen First light APP.</p>
    <p id="work-heavy2" class="hidden">You have chosen Second light APP.</p>
    <p id="work-heavy3" class="hidden">You have chosen Last light APP.</p>

</div>

Database table:

laptop_id - laptop_name - laptop_img - laptop_url - laptop_ram - laptop_graphcard - laptop_processor - laptop_batt - etc.

Table content will be like:

laptop_id - laptop_name - laptop_img  - laptop_url  - laptop_ram
01        - asus-x100   - https://img - https://bla - 2GB 

--------------------- EDIT 2 ------------------------------------------------

So I actually got it working, the following is not the same as my example above but the core functionality is this same (which basically is get data via buttons , use that data to filter in the database and output it). What I didn't include is .js changing the POST values, but that is pretty simple.

index.php:

<!DOCTYPE html>
<html>
<head>  
    <title>TESTING</title>
</head>
<body>
<!-------- this form will be replaced by button which .js will define and adjust the value ---->
   <!---- That value will be used to filter in database--->
<form action="getdata.php" method="POST">
<input type='hidden' name='storage' value='1GB'/> 
<input type="submit">
</form>
<!--- Before submitting .js will change the values of the inputs--->

</body>
</html>

getdata.php:

<?php
    include_once 'testing/databaseconn.php';

    $ramResult = $_POST['storage'];
    echo "this is ramResult " . $ramResult . "<br>";
    $sql = "SELECT * FROM laptops WHERE ram= '$ramResult' ;";
    $result = mysqli_query($conn, $sql);

    if ($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
            $urlResult = $row['img'];            
        }
    }
?>

<html>
<head>  
    <title>Result page</title>
</head>
<body>
<p>test2</p>
<p>
    <img src="<?php print $urlResult; ?>">
</p>


</body>
</html>

getdatabaseconn.php:

<?php

$dbServerName = "localhost"; //because of MAMP
$dbUsername = "root";
$dbPassword = "";
$dnName = "sitedatabase";

$conn = mysqli_connect($dbServerName, $dbUsername, $dbPassword, $dnName);

?>
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
number42
  • 107
  • 7
  • 1
    What you need to answer is, are you ok with a search page / response page approach, or do you need it all on the same page? Although ajax is cool, as a newbie I would steer you away from that for now until you get some experience under your belt. Lots of (most?) retail sites use the search/response page approach. So the first page would basically be a form, and the second page would take the submitted GET (don’t use post for search) variables and present the results. – Tim Morton Jan 04 '20 at 14:43
  • 1
    I would advice you to learn about MVC. Laravel framework implements MVC architecture. You could build something like this then with more organized way. It kinda will work like your steps. – sonic Jan 04 '20 at 14:46
  • @TimMorton Exactly, the results would be on the next page. I'm assuming you mean the php GET, right? Would it then be best practises to do the filtering as much as possible in the Jquery script or in the php script? Will most certainly do a deep dive into GET – number42 Jan 04 '20 at 16:42
  • @sonic thanks will have a look at it – number42 Jan 04 '20 at 16:43
  • 1
    For filtering, use both js and php; JavaScript for user convenience and php for server security. For your question, part of the problem is you don’t know what you don’t know ;) I think I have enough to put together something that will point you in the right direction. Be aware that it’ll take some time; my honey-do list is rather large so my time is limited ;) – Tim Morton Jan 05 '20 at 00:57
  • Exactly @TimMorton! A part of being a newbie is also that you don't know what you don't know and therefore get stuck. You would be my hero if you could help me out with it. Ofcourse, take your time! – number42 Jan 06 '20 at 08:23
  • @TimMorton (background info: user clicks a couple buttons which are dynamic and change according to which button is clicked. Buttons carry values and final result is calculated by .js) after digging I think I need to do the following: use a form method=POST with buttons in it (I don't want values in the url). At the end of the buttons, user can submit answers. When answers submitted, .js calculates and changes hidden html values according to buttons chosen and redirect to next page. PHP can get these PHP values, filters database and presents output in the HTML. Would this be the correct way? – number42 Jan 06 '20 at 11:41
  • 1
    According to REST, HTTP methods correspond to verbs: Post => Create, Get => Read, Put/Patch => Update, Delete => Delete. Unfortunately, browsers default to GET and POST. Therefore, a rule of thumb is: Read requests are GET, and anything that changes data is POST. Any time you use a POST, you need to immediately do a redirect after storing/changing the data so that you don't get reposting when a user hits the back button. This is known as the PRG (Post-Redirect-Get) pattern. – Tim Morton Jan 06 '20 at 15:47
  • 1
    Something that will help me help you is to see 1) Step 1 as concisely as possible, and 2) your expected results page. Your steps 3 and 4 don't quite make sense to me; "After processing of the website..." do you mean processing the user input? This would make steps 3 and 4 basically the same thing. And step 5, for that matter. In the meantime, please check out https://stackoverflow.com/questions/58868420/how-to-create-a-new-table-row-when-someone-inputs-something-into-a-form-using-ju/58885790#58885790 for structure ideas and javascript (jquery) concepts – Tim Morton Jan 06 '20 at 16:04
  • Hi @TimMorton! So step 1: Buttons change according to button chosen. Each set of buttons have their own follow up buttons. The transition to the next buttons happens with .js. Also, at every button select, .js increments a value to a variable. E.g.button 1 is selected, variable RAM gets incremented by 2GB by .js. The result page will display, in a table (laptopname, specs, etc.), all the laptops that match the variables (specs) that got counted. So step 3 is .js counting the variables, step 4 is using those values to filtering in database and get those laptops and outputting them. Thanks Tim!! – number42 Jan 06 '20 at 19:35
  • 1
    OK, you're wanting some ajax functionality instead of simple search/response. Here's what I need from you, added to the post: HTML for the display page, not the whole thing, just the part with buttons. Don't worry about javascript. Schema for your database (IE, what field names are). BTW, the post I **intended** to share previously is https://stackoverflow.com/a/59525516/2129574 – Tim Morton Jan 06 '20 at 22:12
  • @TimMorton Awesome link! I wasn't able to find a clear example of the steps of the process. I've also edited my question and included the html buttons and database table. Please let me know if you need anything else. Thanks again Tim! – number42 Jan 08 '20 at 12:18
  • 1
    I’m still having a great deal of trouble trying to connect the dots of your issue. I think the problem is that we’re trying to solve more than one thing at a time. I fail to see how the buttons interact with retrieving records from the database. I hope the links I shared give you sufficient insight on how to send information back and forth. You might want to formulate a new question with a tighter focus. – Tim Morton Jan 10 '20 at 06:06
  • Hi @TimMorton, I actually got it working! I've edited my question (edit 2). The way the buttons work is as following: the decision of what specs of the laptop needs to be searched for in the database (e.g. search for laptops that have 2GB ram and 500GB SSD), is not done by users typing in the specs they want. This decision will be done automatically when they answer a couple of questions (e.g. "are you going to store a lot of data?"). That's why I needed .js to change the values in order to pass them to the database. So I can take off from here but tips are always welcome!! – number42 Jan 10 '20 at 11:22
  • @TimMorton Thanks again for your help, if I can return the favor just let me know – number42 Jan 10 '20 at 11:22
  • 1
    Super! A few parting tips: 1) Putting php variables in a query string is a security risk. Always use prepared statements. See https://phpdelusions.net/mysqli or tor easier use pdo https://phpdelusions.net/pdo#why 2) Research the PRG pattern to avoid back button and reposting issues when using `$_POST` 3) `if($redult->numrows >0)` is completely unnecessary, see https://phpdelusions.net/pdo#count – Tim Morton Jan 10 '20 at 14:14

0 Answers0