Jump to content
Revolutsio

search into tables

Recommended Posts

I have now got a search engine to work, but it only show in normal text, could someone help me get into a table, I have a CSS file but cannot get the search page to print in my table.

 

<div id="main">
<table>
    <thead>
        <tr>
            <th>ID</td>
            <th>GAME</td>
            <th>PLATFORM</td>

        </tr>
    <thead>        

<?php
$output = NULL;
if(isset($_POST['submit'])) {
    //Connect to database
    $mysqli = NEW MySQLi ("localhost","root","","***.**");

    $search = $mysqli->real_escape_string($_POST{'search'});

    //Query the database
    $resultSet = $mysqli->query("SELECT * FROM games WHERE GAME LIKE '$search%'");

    if($resultSet->num_rows >0){
            while($rows = $resultSet->fetch_assoc())
            {
               $id = $rows['ID'];
               $gn = $rows['GAME'];
               $pf = $rows['PLATFORM'];
               $od = $rows['Owned'];?>
            }   
               
                <tr>
                    <td><?php $output .= $id; ?></tr>;
               
               <!-- Game: $gn<br />
               Platform: $pf<br />
               Owned: $od<br /><br />";-->
               

<form method="POST">
<input type="TEXT" name="search" />
<input type="SUBMIT" name="submit" value="Search" />
</form>
</table>
</body>
</html>

Also how can i use my connect file instead of having the connect to database in this file?

Share this post


Link to post
Share on other sites

I don't understand your code at all. You're putting the rows generation outside of the loop so only 1 row gets created. Plus you are putting your form inside the table. The HTML just doesn't make sense. I suggest you read up on how to create tables in HTML.

As for your 2nd question, you create a script that does the connection as a function call and include functions that do your standard queries. Then add that script with require and call those functions from your main script.

Share this post


Link to post
Share on other sites

Revolutsio,
  Before trying to learn PHP you should have a stronger grasp of HTML then you do. I understand your impatience with getting a web site up, but take a weekend to study your coding.

I removed the PHP and corrected your HTML. Take a look and see if this is what you want.

<th> tags need a matching end tag not </td>

TABLES should have <tbody> tags - I added them

Your <form> appears at the top so I assume you want it there, but not needed in the TABLE - coded it differently and put it on top.

You define only three headers so you only have three columns in the table, It looks like you have five things that you want in the table. $output and $od have no place to go so added an Owned header
 I'm eliminating $output you can insert it  were you want.

Your missing a </td> tag.
The end of the PHP is hard to find

 BUT it placed before you really end the WHILE loop. Your also missing the end of your PHP code ?> tag.
	[code]
	<!DOCTYPE html>
	<html>
	<head>
	<meta charset="utf-8" />
	<meta http-equiv="X-UA-Compatible" content="IE=edge" />
	<title>Page Title</title>
	<meta name="viewport" content="width=device-width, initial-scale=1" />
	</head>
	 
	<body>
	<div id="main">
	  <form method="POST">
	    <input type="TEXT" name="search" />
	    <input type="SUBMIT" name="submit" value="Search" />
	  </form>
	<table border=1>
	<thead>
	<tr>
	  <th>ID</th>
	  <th>GAME</th>
	  <th>PLATFORM</th>
	  <th>OWNED</th>
	</tr>
	<thead>      
	<tbody>
	<?php
	  $output = NULL;
	  if(isset($_POST['submit'])) {
	  $mysqli = NEW MySQLi ("localhost","root","","***.**");
	  $search = $mysqli->real_escape_string($_POST{'search'});
	  $resultSet = $mysqli->query("SELECT * FROM games WHERE GAME LIKE '$search%'");
	  if($resultSet->num_rows >0){
	    while($rows = $resultSet->fetch_assoc()){
	      $id = $rows['ID'];
	      $gn = $rows['GAME'];
	      $pf = $rows['PLATFORM'];
	      $od = $rows['Owned'];
	    }   
	 
	    echo "<tr>
	      <td>";
	        $id
	    echo "  </td>
	      <td>";
	        $gn
	    echo "  </td>
	      <td>";
	        $pf
	    echo "  </td>
	      <td>";
	        $od
	      </td>
	    </tr>";
	  }
	?>
	</tbody>  
	</table>
	</body>
	</html>
	</form>
	</body>
	</html>
	

Edited by NotSunfighter

Share this post


Link to post
Share on other sites

I have copied this from a youtube video, I have a good grasp of html. I want a search my database and be able to edit the details on my webpage so I went looking for help and found this code that did search my database and print the results to the screen and this worked. but when I tried to add my css file and other bits from my site i get errors. 

Share this post


Link to post
Share on other sites

try

<?php
require 'db_inc.php';
$db = pdoConnect();            // function defined in db_inc.php

$search = $_GET['search'] ?? '';
$tdata = '';

$stmt = $db->prepare("SELECT ID
                           , GAME
                           , PLATFORM
                           , Owned
                      FROM games
                      WHERE GAME LIKE ?     
                     ");
$stmt->execute([ "$search%" ]);
foreach ($stmt as $row) {
    $tdata .= "<tr><td>" . join('</td><td>', $row) . "</td></tr>\n";
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Sample Search</title>
<style type='text/css'>
    body  { font-family: calibri, sans-serif; font-size: 11pt;}
    header{ background-color: black; color: white; padding: 16px; }
    form  { padding: 16px; }
    table { width: 75%; margin: 30px auto; border-collapse: collapse; }
    th    { background-color: black; color: white; padding: 8px; }
    td    { padding: 4px 8px;}
</style>
</head>
<body>
<header>
    <h1>Sample</h1>
</header>
<form method='GET'>
    Search for: 
    <input type="text" name="search" value="<?=$search?>" size="40">
    <input type="submit" name="btnSub" value="Search"> 
</form>
<div>
<?php if ($search) { ?>
   <table border='1'>
       <thead>
           <tr>
              <th>ID</th>
              <th>GAME</th>
              <th>PLATFORM</th>
              <th>OWNED</th>
            </tr>
       </thead>
       <tbody>
           <?=$tdata?>
       </tbody>
   </table>
<?php } ?>
</div>
</body>
</html>

 

Share this post


Link to post
Share on other sites

Revolutsio,
  I see where you have used "this is not my code I found this on youtube" on your questions before. Most YT teaching vids never post code - it has to be copied. If this is how your doing it, please verify your randision before asking for help. Chances are pretty high that the correct code, as was given on youtube, works.

Share this post


Link to post
Share on other sites
On 1/12/2020 at 4:39 PM, Barand said:

try


<?php
require 'db_inc.php';
$db = pdoConnect();            // function defined in db_inc.php

$search = $_GET['search'] ?? '';
$tdata = '';

$stmt = $db->prepare("SELECT ID
                           , GAME
                           , PLATFORM
                           , Owned
                      FROM games
                      WHERE GAME LIKE ?     
                     ");
$stmt->execute([ "$search%" ]);
foreach ($stmt as $row) {
    $tdata .= "<tr><td>" . join('</td><td>', $row) . "</td></tr>\n";
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Sample Search</title>
<style type='text/css'>
    body  { font-family: calibri, sans-serif; font-size: 11pt;}
    header{ background-color: black; color: white; padding: 16px; }
    form  { padding: 16px; }
    table { width: 75%; margin: 30px auto; border-collapse: collapse; }
    th    { background-color: black; color: white; padding: 8px; }
    td    { padding: 4px 8px;}
</style>
</head>
<body>
<header>
    <h1>Sample</h1>
</header>
<form method='GET'>
    Search for: 
    <input type="text" name="search" value="<?=$search?>" size="40">
    <input type="submit" name="btnSub" value="Search"> 
</form>
<div>
<?php if ($search) { ?>
   <table border='1'>
       <thead>
           <tr>
              <th>ID</th>
              <th>GAME</th>
              <th>PLATFORM</th>
              <th>OWNED</th>
            </tr>
       </thead>
       <tbody>
           <?=$tdata?>
       </tbody>
   </table>
<?php } ?>
</div>
</body>
</html>

 

Sorry for the late reply I have tried the code and I get an error in line 3

 

Call to undefined function pdoConnect()

Here is my loggin code called 'dbh.php'

 

<?php

$dbServername = "localhost";
$dbUsername = "root";
$dbPassword = "";
$dbName = "csv_db";

$conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);
?>

 

Share this post


Link to post
Share on other sites
On 1/12/2020 at 6:08 PM, NotSunfighter said:

Revolutsio,
  I see where you have used "this is not my code I found this on youtube" on your questions before. Most YT teaching vids never post code - it has to be copied. If this is how your doing it, please verify your randision before asking for help. Chances are pretty high that the correct code, as was given on youtube, works.

The code did work my question was how do I put the results in a table

Share this post


Link to post
Share on other sites
16 minutes ago, Revolutsio said:

The code did work

because you needed a PDO connection

 

16 minutes ago, Revolutsio said:

my question was how do I put the results in a table

and that is what it does 

 

Here's a mysqli version

<?php
$dbServername = "localhost";
$dbUsername = "root";
$dbPassword = "";
$dbName = "csv_db";

$conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);
$search = $_GET['search'] ?? '';
$tdata = '';

$stmt = $conn->prepare("SELECT ID
                           , GAME
                           , PLATFORM
                           , Owned
                      FROM games
                      WHERE GAME LIKE ?     
                     ");
$srchStr = "$search%";
$stmt->bind_param('s', $srchStr);
$stmt->execute();
$row = [];
$stmt->bind_result($row[0],$row[1],$row[2],$row[3]);
while ($stmt->fetch()) {
    $tdata .= "<tr><td>" . join('</td><td>', $row) . "</td></tr>\n";
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Sample Search</title>
<style type='text/css'>
    body  { font-family: calibri, sans-serif; font-size: 11pt;}
    header{ background-color: black; color: white; padding: 16px; }
    form  { padding: 16px; }
    table { width: 75%; margin: 30px auto; border-collapse: collapse; }
    th    { background-color: black; color: white; padding: 8px; }
    td    { padding: 4px 8px;}
</style>
</head>
<body>
<header>
    <h1>Sample</h1>
</header>
<form method='GET'>
    Search for: 
    <input type="text" name="search" value="<?=$search?>" size="40">
    <input type="submit" name="btnSub" value="Search"> 
</form>
<div>
<?php if ($search) { ?>
   <table border='1'>
       <thead>
           <tr>
              <th>ID</th>
              <th>GAME</th>
              <th>PLATFORM</th>
              <th>OWNED</th>
            </tr>
       </thead>
       <tbody>
           <?=$tdata?>
       </tbody>
   </table>
<?php } ?>
</div>
</body>
</html>

 

Share this post


Link to post
Share on other sites

Sorry Barand the second reply wasn't for you 

 

Thank you for your code the second one works for me

Edited by Revolutsio
MORE INFORMATION

Share this post


Link to post
Share on other sites

If I haven't said it to you already, I recommend you change to PDO. It's more streamlined and easier to use than mysqli. (As you can see, it took twice as much code to process the query)

Edited by Barand

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.