Counting table rows in PHP - php

I am working on a project and I need to find a way to count the table rows when a user has selected a Group from the drop-down box. I have tried some ways in which I can show, but also I am not grasping how this concept would work. I am also using an SQL database which works perfectly, the rows is giving me a hard time though.
<!DOCTYPE html>
<html>
<body>
<form action="Web4.php" method="post">
<label form="return">Group: </label>
<select id="return" name="return">
<option value="G">All</option>
<option value="A">Group A</option>
<option value="B">Group B</option>
<option value="C">Group C</option>
<option value="D">Group D</option>
<option value="E">Group E</option>
</select>
<input type="submit">
</form>
<?php
$servername = "servername";
$username = "myusername";
$password = "mypassword";
$dbname = "mydatabasename";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection Failed: ". $conn->connect_error);
}
if(!isset($_POST["return"])) {$selval ="G";}
else {$selval = $_POST["return"];}
if($selval == "G"){
$scores = mysqli_query($conn, "select * from test_scores;");
}
elseif($selval == "A"){
$scores = mysqli_query($conn, "select * from test_scores WHERE group_id='A';");
}
elseif($selval == "B"){
$scores = mysqli_query($conn, "select * from test_scores WHERE group_id='B';");
}
elseif($selval == "C"){
$scores = mysqli_query($conn, "select * from test_scores WHERE group_id='C';");
}
elseif($selval == "D"){
$scores = mysqli_query($conn, "select * from test_scores WHERE group_id='D';");
}
else{
$scores = mysqli_query($conn, "select * from test_scores WHERE group_id='E';");
}
?>
<table border="1">
<tr><th>Record ID</th><th>Math Score</th><th>Reading Score</th><th>Writing Score</th></td></tr>
<?php
while($row = mysqli_fetch_array($scores)){
echo "<tr><td>".$row["group_id"]."</td><td>".$row["math"]."</td><td>".$row["reading"]."</td><td>".$row["writing"]."</td></tr>\n";
}
?>
</table>
</body>
</html>

Related

for loop over selection option value HTML with PHP

Hi I work like loop through the selection option values with PHP instead of duplicating it, from value 500 to 510. Can you kindly show me how to do it?
<body>
<div class="form-group">
<br> <label>Job Description</label>: <b><?php echo $row['job_desc']; ?><br></b>
<select class="form-control" name="job_code">
<option value="500">System Analysis</option>
<option value="501">Programmer</option>
<option value="502">Database Designer</option>
<option value="503">Electrical Engineer</option>
<option value="504">Mechanical Engineer</option>
<option value="505">Civil Engineer</option>
<option value="506">Clerical Support</option>
<option value="507">DSS Analyst</option>
<option value="508">Application Designer</option>
<option value="509">Bio Technician</option>
<option value="510">General Support</option>
</select>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary" name="update" value="Update Data">Save</button>
</div>
</body>
Employee BDJob DB
<?php
$connection = mysqli_connect("localhost", "root", "");
$db = mysqli_select_db($connection, 'amaz');
if (isset($_POST['update'])) {
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$sex = $_POST['sex'];
$emp_salary = $_POST['emp_salary'];
$dept_name = $_POST['dept_name'];
$job_code = $_POST['job_code'];
$query = "UPDATE employee
SET employee.first_name='$_POST[first_name]',employee.last_name='$_POST[last_name]',employee.sex='$_POST[sex]',employee.emp_salary='$_POST[emp_salary]',employee.dept_id='$_POST[dept_name]',employee.job_code='$_POST[job_code]'
WHERE employee.emp_num='$_POST[id]'";
$query_run = mysqli_query($connection, $query);
}
?>
You make a SELECT query that looks at the jobs table:
$server = 'localhost';
$user = 'root';
$pass = '';
$db = 'amaz';
$mysql = new mysqli($server, $user, $pass, $db);
if ($mysql->connect_error !== null) {
printf("Connect failed: %s\n", $mysql->connect_error);
exit;
}
$select = $mysql->query("SELECT * FROM jobs WHERE job_code > 409 and job_code < 511");
$jobs = [];
while ($row = $select->fetch_array(MYSQLI_ASSOC)) {
$jobs[] = $row;
}
?>
<select class="form-control" name="job_code">
<?php foreach ($jobs as $job): ?>
<option value="<?=$job['job_code']?>"><?=$job['job_desc']?></option>
<?php endforeach; ?>
</select>
This will output:
<select class="form-control" name="job_code">
<option value="500">System Analysis</option>
<option value="501">Programmer</option>
<option value="502">Database Designer</option>
...etc
</select>

Inserting selected value from populated mysql dropdown

I am trying to insert the selected value from a drop down that was populated from a reference table in my database. I followed a tutorial for a dynamic dropdown but now I would like to take the value and insert it. The problem is it keeps taking the echo the tutorial uses. Is there a way I can make that selected value a new variable? It currently inserts "< php echo $team_name"
<div>
<label>Home Team</label>
<select name="home_team" style="width:125px;>
<option value="">Select Team</option>
<?php
$query = "SELECT * FROM team";
$results = mysqli_query($db, $query);
mysqli_query($db, "SELECT * FROM team_name");
// loop
foreach ($results as $team_name) {
?>
<option value="<php echo $team_name["cid"]; ?><?php echo $team_name["team_name"]; ?></option>
<?php
}
?>
</select>
How I attempted to insert:
$db = mysqli_connect('localhost', 'root', 'root', 'register');
if(mysqli_connect_errno())
{
echo "failed" . mysqli_connect_error();
}
//var_dump($_POST);
$home_team = mysqli_real_escape_string($db, $_POST['home_team']);
$home_team = $home_team;
$query = "INSERT INTO game_table (home_team)
VALUES('$home_team')";
mysqli_query($db, $query);
//echo $query;
//echo $home_team;
//header('location: index.php');
please follow this.
<select name="home_team" style="width:125px;>
<option value="">Select Team</option>
<?php
$query = "SELECT * FROM team";
$results = mysqli_query($db, $query);
while($row = mysqli_fetch_assoc($results)) {
?>
<option value="<php echo $row['cid']; ?>"><?php echo $row["team_name"]; ?></option>
<?php } ?>
</select>
may be this should work
Try this. There was a couple of missing " and ? in your code.
<select name="home_team" style="width:125px;">
<option value="">Select Team</option>
<?php
$query = "SELECT * FROM team";
$results = mysqli_query($db, $query);
foreach ($row = mysqli_fetch_assoc($results)) {
?>
<option value="<?php echo $row["cid"]; ?>">
<?php echo $row["team_name"]; ?>
</option>
<?php
}
?>
</select>

Calling a PHP function with HTML variable

When the combobox is select, i'm trying to display first n items from database
i'm trying to call the PHP function "produse($_POST)" with the value of the selected item in the select tag, the code is correct?
the limit in the SELECT SQL can be the one in the code? ("$sql = "SELECT * FROM table LIMIT $number";")
<body>
<select name="n" onchange="document.write('<?php produse($_POST); ?>')">
<option disabled selected value> -- select an option -- </option>
<option value=2>2</option>
<option value=3>3</option>
<option value=4>4</option>
</select>
<br> <br> <br>
<?php
function produse($number){
$servername = "localhost:3306";
$username = "root";
$password = "";
$dbname = "produse";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$number = intval($_GET['number']);
$sql = "SELECT * FROM table LIMIT $number";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "id_produs: " . $row["id_produs"]. " - Denumire: " . $row["Denumire"]. " - Pret " . $row["Pret"]. " - Descriere" . $row["Descriere"] ."<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
}
?>
</body>
Use it separately using jQuery like this:
HTML (html_page.html) :
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Your HTML Page title</title>
</head>
<body>
<div>
<select id="my_select">
<option value="" selected>Choose</option>
<option value="1">Option1</option>
<option value="2">Option2</option>
</select>
</div>
<hr />
<div class="dynamic_content"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$('#my_select').change(function() {
showResult($(this).val());
});
function showResult(value) {
if (value) {
$('.dynamic_content').load('php_page.php?number=' + value);
}
}
</script>
</body>
</html>
PHP Page (php_page.php):
<?php
//intval($_GET['item']) will return 0 and considered as FALSE if the requested parameter is not a number
if (isset($_GET['number']) && !empty($_GET['number']) && intval($_GET['number']) {
$number = intval($_GET['number']);
//I DID NOT TEST YOUR PHP CODE
$servername = "localhost:3306";
$username = "root";
$password = "";
$dbname = "produse";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM table LIMIT $number";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "id_produs: " . $row["id_produs"]. " - Denumire: " . $row["Denumire"]. " - Pret " . $row["Pret"]. " - Descriere" . $row["Descriere"] ."<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
}
?>

php - Drop Down Executes Query On Post?

I've looked online and most drop down tutorials are for ratings e.g. matching the drop down value with a rating. I need to execute a query which corresponds to a number in the dropdown and the results to display once a user clicks submit, I don't want to use javascript. In my HTML :
<form action="" method="post" enctype="multipart/form-data" name="form1" id="genericForm">
<fieldset>
<p>Filter Rating</p>
<select name="value">
<option value="1">One Star</option>
<option value="2">Two Stars</option>
<option value="3">Three Stars</option>
<option value="4">Four Stars</option>
<option value="5">Five Stars</option>
</select>
</div>
<input type="submit" name="Submit" value="Submit"><br />
</form>
The php :
<?php
$Link = mysql_connect($Host, $User, $Password);
if($_POST['value'] == '1') {
// query to get all 1 star ratings
$query = "SELECT * FROM films WHERE genre='action' AND rating='1'";
}
elseif($_POST['value'] == '2') {
// query to get all 2 star ratings
$query = "SELECT * FROM films WHERE genre='action' AND rating='2'";
}
elseif($_POST['value'] == '3') {
// query to get all 3 star ratings
$query = "SELECT * FROM films WHERE genre='action' AND rating='3'";
}
elseif($_POST['value'] == '4') {
// query to get all 4 star ratings
$query = "SELECT * FROM films WHERE genre='action' AND rating='4'";
}
elseif($_POST['value'] == '5') {
// query to get all 5 star ratings
$query = "SELECT * FROM films WHERE genre='action' AND rating='5'";
}
WHILE($board = mysql_fetch_array($result)):
$title = $board['title'];
$studio = $board['studio'];
$language = $board['language'];
$certification = $board['certification'];
echo '
Title : '.$title.'<br />
Studio : '.$studio.'<br />
Language : '.$language.'<br />
Certification : '.$certification.'<br />
;
endwhile;
?>
Try it this way, assuming you're already connected and have selected DB and that you're using your entire code inside the same file, since you are using action=""; this denotes executing as "self".
You also are not executing mysql_query() which I have added below.
Be sure to change xxx below with your DB credentials and your_db to your database's name.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$Host = "xxx";
$User = "xxx";
$Password = "xxx";
$Link = mysql_connect($Host, $User, $Password);
$db_selected = mysql_select_db('your_db', $Link);
if (!$db_selected) {
die ('Can\'t use that DB : ' . mysql_error());
}
if(isset($_POST['Submit'])){
if($_POST['value'] == '1') {
// query to get all 1 star ratings
$query = mysql_query("SELECT * FROM films WHERE genre='action' AND rating='1'");
}
elseif($_POST['value'] == '2') {
// query to get all 2 star ratings
$query = mysql_query("SELECT * FROM films WHERE genre='action' AND rating='2'");
}
elseif($_POST['value'] == '3') {
// query to get all 3 star ratings
$query = mysql_query("SELECT * FROM films WHERE genre='action' AND rating='3'");
}
elseif($_POST['value'] == '4') {
// query to get all 4 star ratings
$query = mysql_query("SELECT * FROM films WHERE genre='action' AND rating='4'");
}
elseif($_POST['value'] == '5') {
// query to get all 5 star ratings
$query = mysql_query("SELECT * FROM films WHERE genre='action' AND rating='5'");
}
WHILE($board = mysql_fetch_array($query)){
$title = $board['title'];
$studio = $board['studio'];
$language = $board['language'];
$certification = $board['certification'];
echo '
Title : '.$title.'<br />
Studio : '.$studio.'<br />
Language : '.$language.'<br />
Certification : '.$certification.'<br />';
}
} // brace for if(isset($_POST['Submit']))
?>
</div>
<form action="" method="post" enctype="multipart/form-data" name="form1" id="genericForm">
<fieldset>
<p>Filter Rating</p>
<select name="value">
<option value="1">One Star</option>
<option value="2">Two Stars</option>
<option value="3">Three Stars</option>
<option value="4">Four Stars</option>
<option value="5">Five Stars</option>
</select>
</div>
<input type="submit" name="Submit" value="Submit"><br />
</form>
Note:
This enctype="multipart/form-data" isn't required if it's not going to be used to upload files.
If im not mistaken this is what you want? you just need to loop through the row in the tables and fetch it.
<div class="form">
<?php
$Link = mysql_connect($Host, $User, $Password);
$Query = "select * from books where product = 'hannibal' AND book = '1'";
$result = mysql_query($Query);
while ($row = mysql_fetch_array($result)) {
$subject = $row['subject'];
$username = $row['username'];
$date = $row['date'];
$comments = $row['comments'];
?>
<h1>Subject : <?php echo $subject;?>
Posted by <?php echo $username;?> on <?php echo $date;?></h1><br />
<?php echo $comments;?>'; <br />
<?php
}
?>
</div>

Get value of mysql database in select menu

I have issue with select menu on PHP. I tried to get mysql database to select menu. However, it displays none.
Here is my code:
default:
mysql_select_db($database_conn, $conn);
$query_Rsenroll = "SELECT * FROM `tbl_enroll` WHERE `tbl_enroll`.`courseid` ='".$_GET['courseid']."'";
$Rsenroll = mysql_query($query_Rsenroll, $conn) or die(mysql_error());
$row_Rsenroll = mysql_fetch_assoc($Rsenroll);
$totalRows_Rsenroll = mysql_num_rows($Rsenroll);
$courseid = $row_Rsenroll['courseid'];
$er_staffid = "";
break;
}
?>
<select name="courseid">
<option value="" SELECTED>Selected Course ID</option>
<?php
foreach( $Course as $course_id) {
if ( $course_id == $courseid) {
$selected = " SELECTED";
} else {
$selected = "";
}
?>
<option value="<?php echo $course_id; ?>"<?php echo $selected; ?>><?php echo $row_Rsenroll['courseid']; ?></option>
<?php
}
?>
</select>
Thank you for any help and advice.
Assuming courseid is being passed as a variable in the sending URL (file.php?courseid=COURSEID), I think this should do what you want:
This might clean up your script a little (though I switched it to mysql_fetch_array as I'm more familiar with that than mysql_fetch_assoc. Feel free to use assoc):
<?php
$cid = '6116';
?>
<select name="courseidMenu">
<option value="" SELECTED>Selected Course ID</option>
<?php
$query = mysql_query("SELECT * FROM tbl_enroll WHERE courseid = '$cid'", $conn)or die(mysql_error());
$total_rows = mysql_num_rows($query);
while($row = mysql_fetch_array($query)){
$courseId = $row['courseid'];
?>
<option value="<?=$courseId?>" ><?=$courseId?></option>
<?
}
?>
</select>
updated use this it is working on my portal <select>
<option value=''>Select Provider</option>
<?php
$server="server name";
$user="user name";
$password="password";
$database="database";
$conn=mysql_connect($server,$user,$password) or die("connection failed");
mysql_select_db($database,$conn);
$query_Rsenroll = "SELECT * FROM `tbl_enroll` WHERE `tbl_enroll`.`courseid` ='".$_GET['courseid']."'";
$result= mysql_query($query_Rsenroll, $conn) or die(mysql_error());
$n=mysql_num_rows($result);
if($n>0)
while($row=mysql_fetch_array($rs))
echo"<option value='$row['courseid']'>$row['courseid']</option>";
mysql_close($conn);
?>

Categories