webdeveloper001 Posted September 23, 2018 Share Posted September 23, 2018 Hi All Iam Already Created My Own Php Script As Below <?Php echo "<form method=post action=''><input type=hidden name=todo value=search>"; $q="select distinct class from student"; echo "<select class=form-control name=class><option value=''>Any Class</option>"; foreach ($dbo->query($q) as $n) { echo "<option value=$n[class]>$n[class]</option>"; } echo "</select>"; echo "<br>"; $qq="select distinct sex from student"; echo "<select class=form-control name=sex><option value=''>choose</option>"; foreach ($dbo->query($qq) as $nn) { echo "<option value=$nn[sex]>$nn[sex]</option>"; } echo "</select>"; echo " <br><input type=submit value=Search> </form> "; $todo=$_POST['todo']; if(isset($todo) and $todo=="search"){ $class=$_POST['class']; $sex=$_POST['sex']; $query="select * from student where "; if(strlen($class) > 0 ){ $query.= " class='$class' and "; } if(strlen($sex) > 0 ){ $query.= " sex='$sex' and "; } $query=substr($query,0,(strLen($query)-4)); echo "<span style=\"background-color: #F0F000\">$query</span>"; echo "<br><br>"; // Display records //// foreach ($dbo->query($query) as $t) { echo "$t[id] , $t[name],$t[class],$t[mark],$t[sex]<br>"; } } ?> And I want To Replace Display Records This Lines // Display records //// foreach ($dbo->query($query) as $t) { echo "$t[id] , $t[name],$t[class],$t[mark],$t[sex]<br>"; With This Table <div class="container"> <table class="table table-bordered table-hover table-striped"> <thead> <tr> <th class="col-md-1">Name</th> <th class="col-md-1">Class</th> <th class="col-md-1">Session Based</th> <th class="col-md-1">Mark</th> <th class="col-md-2">Sex</th> <th class="col-md-2">Phone</th> <th class="col-md-2">Date</th> </tr> </thead> <tbody> <tr> <td>John Doe</td> <td>Four</td> <td>False</td> <td>452</td> <td>Male</td> <td>0121212121</td> <td>01 January</td> </tr> <tr> <td>Anna Meghan</td> <td>Five</td> <td>False</td> <td>377</td> <td>Female</td> <td>032132100</td> <td>04 March</td> </tr> </tbody> </table> </div> Any Support Please Quote Link to comment Share on other sites More sharing options...
ginerjm Posted September 23, 2018 Share Posted September 23, 2018 (edited) Your html code is terribly flawed. Awful!!! Attributes must be enclosed in quotes. Here is one example of a corrected line of html. You wrote: "<form method=post action=''>" Should be: "<form method='post' action=''>" You have lots of learning to do apparently. Edited September 23, 2018 by ginerjm 1 Quote Link to comment Share on other sites More sharing options...
Barand Posted September 23, 2018 Share Posted September 23, 2018 In addition to @ginerjm's advice Separate your php from your html as much as possible, avaiding your spaghetti-like code structure. Do your php processing first, followed by the html output Don't use "select star", specify the fields you want. Don't put user privided data directly into your query strings, use prepared statements I have rewritten your page to illustrate these points <?php $tdata = ''; $where = []; $whereclause = ''; $params = []; $class = $_GET['class'] ?? ''; $sex = $_GET['sex'] ?? ''; if (isset($_GET['todo']) && $_GET['todo']=='search') { if ($class) { $where[] = "class = ?"; $params[] = $class; } if ($sex) { $where[] = "sex = ?"; $params[] = $sex; } if ($where) { $whereclause = " WHERE " . join(' AND ', $where); } } $stmt = $dbo->prepare("SELECT name , class , session_based , mark , sex , phone , date FROM student $whereclause"); $stmt->execute($params); foreach ($stmt as $row) { $tdata .= "<tr><td>" . join('</td><td>', $row) . "</td></tr>\n"; } function classOptions($dbo, $current='') { $opts = ''; $res = $dbo->query("SELECT DISTINCT class FROM student"); foreach ($res as $r) { $sel = $r['class']==$current ? 'selected' : ''; $opts .= "<option $sel value='{$r['class']}'>{$r['class']}</option>\n"; } return $opts; } function sexOptions($dbo, $current='') { $opts = ''; $res = $dbo->query("SELECT DISTINCT sex FROM student"); foreach ($res as $r) { $sel = $r['sex']==$current ? 'selected' : ''; $opts .= "<option $sel value='{$r['sex']}'>{$r['sex']}</option>\n"; } return $opts; } ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="generator" content="PhpED 18.0 (Build 18044, 64bit)"> <title>Example</title> <meta name="author" content="Barand"> <meta name="creation-date" content="09/23/2018"> </head> <body> <form method='get' action=''> <input type="hidden" name="todo" value="search"> Class <select class="form-control" name="class"> <option value=''>Any Class</option> <?=classOptions($dbo, $class)?> </select> <br> Sex <select class="form-control" name="sex"> <option value=''>All</option> <?=sexOptions($dbo, $sex)?> </select> <br> <input type="submit" value="Search"> <input type="reset" value="Reset"> </form> <br><br> <div class="container"> <table class="table table-bordered table-hover table-striped"> <thead> <tr> <th class="col-md-1">Name</th> <th class="col-md-1">Class</th> <th class="col-md-1">Session Based</th> <th class="col-md-1">Mark</th> <th class="col-md-2">Sex</th> <th class="col-md-2">Phone</th> <th class="col-md-2">Date</th> </tr> </thead> <tbody> <?=$tdata?> </tbody> </table> </div> </body> </html> 1 Quote Link to comment Share on other sites More sharing options...
webdeveloper001 Posted September 23, 2018 Author Share Posted September 23, 2018 26 minutes ago, Barand said: In addition to @ginerjm's advice Separate your php from your html as much as possible, avaiding your spaghetti-like code structure. Do your php processing first, followed by the html output Don't use "select star", specify the fields you want. Don't put user privided data directly into your query strings, use prepared statements I have rewritten your page to illustrate these points <?php $tdata = ''; $where = []; $whereclause = ''; $params = []; $class = $_GET['class'] ?? ''; $sex = $_GET['sex'] ?? ''; if (isset($_GET['todo']) && $_GET['todo']=='search') { if ($class) { $where[] = "class = ?"; $params[] = $class; } if ($sex) { $where[] = "sex = ?"; $params[] = $sex; } if ($where) { $whereclause = " WHERE " . join(' AND ', $where); } } $stmt = $dbo->prepare("SELECT name , class , session_based , mark , sex , phone , date FROM student $whereclause"); $stmt->execute($params); foreach ($stmt as $row) { $tdata .= "<tr><td>" . join('</td><td>', $row) . "</td></tr>\n"; } function classOptions($dbo, $current='') { $opts = ''; $res = $dbo->query("SELECT DISTINCT class FROM student"); foreach ($res as $r) { $sel = $r['class']==$current ? 'selected' : ''; $opts .= "<option $sel value='{$r['class']}'>{$r['class']}</option>\n"; } return $opts; } function sexOptions($dbo, $current='') { $opts = ''; $res = $dbo->query("SELECT DISTINCT sex FROM student"); foreach ($res as $r) { $sel = $r['sex']==$current ? 'selected' : ''; $opts .= "<option $sel value='{$r['sex']}'>{$r['sex']}</option>\n"; } return $opts; } ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="generator" content="PhpED 18.0 (Build 18044, 64bit)"> <title>Example</title> <meta name="author" content="Barand"> <meta name="creation-date" content="09/23/2018"> </head> <body> <form method='get' action=''> <input type="hidden" name="todo" value="search"> Class <select class="form-control" name="class"> <option value=''>Any Class</option> <?=classOptions($dbo, $class)?> </select> <br> Sex <select class="form-control" name="sex"> <option value=''>All</option> <?=sexOptions($dbo, $sex)?> </select> <br> <input type="submit" value="Search"> <input type="reset" value="Reset"> </form> <br><br> <div class="container"> <table class="table table-bordered table-hover table-striped"> <thead> <tr> <th class="col-md-1">Name</th> <th class="col-md-1">Class</th> <th class="col-md-1">Session Based</th> <th class="col-md-1">Mark</th> <th class="col-md-2">Sex</th> <th class="col-md-2">Phone</th> <th class="col-md-2">Date</th> </tr> </thead> <tbody> <?=$tdata?> </tbody> </table> </div> </body> </html> Thanks For Your Great Support I got All The Points And Advises The Most Important One Is To separate my Html And My PHP And About This lines it locate my database and how i could fill it the right way ? $tdata = ''; $where = [localhost]; $whereclause = 'bbank'; $params = []; Thanks Quote Link to comment Share on other sites More sharing options...
webdeveloper001 Posted September 23, 2018 Author Share Posted September 23, 2018 41 minutes ago, ginerjm said: Your html code is terribly flawed. Awful!!! Attributes must be enclosed in quotes. Here is one example of a corrected line of html. You wrote: "<form method=post action=''>" Should be: "<form method='post' action=''>" You have lots of learning to do apparently. Thank You Very Much For Ur Advises Quote Link to comment Share on other sites More sharing options...
Barand Posted September 23, 2018 Share Posted September 23, 2018 1 hour ago, webdeveloper001 said: And About This lines it locate my database and how i could fill it the right way ? $tdata = ''; $where = [localhost]; $whereclause = 'bbank'; $params = []; Those lines are to initialise the empty variables. They are nothing to do with database connection. My connection code, which would come before those lines (or in an included file as I do), is $host = '????'; // $username = '????'; // $password = '????'; // provide your credentials $database = '????'; // $dsn = "mysql:dbname=$database; host=$host; charset=utf8"; $dbo = new pdo($dsn, $username, $password, [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC ]); 1 Quote Link to comment Share on other sites More sharing options...
phpmillion Posted September 23, 2018 Share Posted September 23, 2018 1 hour ago, webdeveloper001 said: The Most Important One Is To separate my Html And My PHP In short, you can (and should) use PHP templating system. Since you are new to PHP, it might look difficult first, but once you see some examples and try to create your own code, you will see it's not that difficult... Plus, such a method will enable you to maintain your code easier and faster (which is very important if you decide to create more advanced scripts in future). There are many PHP templating systems around, but I think Twig is one of the easiest systems to learn. Other developers might have a different opinion on this, so take your time to review different templating systems and choose the one you like most. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted September 23, 2018 Share Posted September 23, 2018 OTOH - it is not really that hard to write properly structured code. In fact a little discipline now that you know that it should be done would be a good teaching experience for you as a beginner. Sure you may write some html into your php processing, such as when building the contents of an html table from the results of a query. In those cases you will gather the data and the html elements into a single php variable and then place that var into the bulk of your complete html page output which will all be somewhere else (the end?) in your script. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.