How to join 2 tables no foreignkey but same data's? - php

Is it possible to join to tables like in the following picture
There are no foreign key in svotes but there are the same records. In the select option tag I putted the school_year and the option are 2015-2016,2016-2017. If I click the 2016 it should that the other table svotes can display the year of 2016 can it be possible? and how?
Here's my code:
<select name="sy_no" onchange="getyr(this.value)">
<option></option>
<?php
include('../connect.php');
$sql2 = mysql_query("SELECT * FROM school_year");
while ($row1=mysql_fetch_array($sql2)) {
echo "<option value=".$row1['syearid'].">".$row1['from_year'].'-'.$row1['to_year']."</option>";
}
?>
</select>
get_winner.php
<?php
include('../connect.php');
$no = $_GET['no'];
//this is where I get the year
$stud = mysql_query("SELECT * FROM studentvotes,school_year WHERE school_year.from_year = studentvotes.year AND studentvotes.year = '$no' ") or die(mysql_error());
echo"<center>";
echo "<form action='' method='POST' enctype='multipart/form-data' >";
echo " <table class='table table-striped table-bordered table-hover' id='dataTables-example'>";
echo "<tr>";
echo "<th>IDno</th>";
echo "<th>Action</th>";
echo "</tr>";
while ($row=mysql_fetch_array($stud)) {
$result = mysql_query("SELECT * FROM studenvotes,school_year WHERE studenvotes.year = school_year.from_year") or die(mysql_error());
$r1 = mysql_fetch_assoc($result);
?>
<tr class="headings">
<?php
}
?>

Try this join query
<select name="sy_no" onchange="getyr(this.value)">
<option></option>
<?php
include('../connect.php');
$sql2 = mysql_query("SELECT school_year.*,svotes.* FROM school_year JOIN svotes ON , svotes.year = school_year.from_year ");
while ($row1=mysql_fetch_array($sql2)) {
echo "<option value=".$row1['syearid'].">".$row1['from_year'].'-'.$row1['to_year']."</option>";
}
?>
</select>

Related

search data and will choose to display in static table. like an add to cart

I want to display data after in static table just like an "Add to Cart".
<table>
<th>Item</th> <th>Description</th>
<tr>
<td>Monitor<td>
<td>Monitor Blue<td>
</tr>
<tr>
<td>Mouse<td>
<td>Mouse Wireless<td>
</tr>
<tr>
<td>Keyboard<td>
<td>Keyboard<td>
</tr>
My search form : This is no problem is searching item. Only in the add button every row. So when you search for item you have choice if you want to add in static table.
Search:
Mysql when search
<?
$sql = "select * from item_tb where name='".$search."'";
$result=$conn->query($sql);
echo "
<table cellspacing='20' border='0' ><tr> <th>Item</th><th>Description</th>
</tr>";
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "
<tr> <td>
".$row["name"]."</td> <td>
".$row["description"]."</td>";
echo" <form name='add' method='POST'>";
echo" <td><a href='item.php?id=".$row['Iid']."'><input type='submit' value='Add'></td></a>";
echo" </form>";
}
}
?>
Here is my main problem. i don't how to make table where i'll get the data i have search and will display as many as item i add. I'm really down in this.
<?
if($id != "")
{
$sql = "SELECT * FROM item_tb WHERE Iid='".$id."'";
$result=$conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo" ".$row["name"]." ";
}
}
}
?>
<?php
if($id != "") {
$sql = "SELECT * FROM item_tb WHERE Iid='" . $id . "'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = mysql_fetch_array($result)) {
?>
<tr>
<td><?= $row['name'] ?></td>
</tr>
<?php
}
}
}
?>

PHP dropdown database code not working?

I have an issue with the below code, it is showing the html part but not the data from my database. I am just trying to have the data from the database in the drop down list and when I click find to display it on the table below. Can you please help?
<?php
include 'config.php';
?>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<select name="manufacturer">
<option value="">----ALL----</option>
<?php
$sql = "SELECT id, manufacturer FROM coop";
$result = $conn->query($sql);
while ($r = mysqli_fetch_assoc($result))
{
echo "<option value='$r[id]'> $r[manufacturer]</option>";
}
?>
</select>
<input type="submit" name="find" value="find"/>
<br><br>
<table border="1">
<tr align="center">
<th>ID</th> <th>Manufacturer</th>
</tr>
<?php
if($_SERVER['REQUEST_METHOD'] == "POST")
{
$des=$_POST["manufacturer"];
if($des=="")
{
$result= mysqli_query("SELECT id, manufacturer FROM coop");
}
else
{
$result= mysqli_query($sql);
}
echo "<tr><td colspan='5'></td></tr>";
while($r= mysqli_fetch_assoc($result))
{
echo "<tr>";
echo "<td align='center'>$r[0]</td>";
echo "<td width='200'>$r[1]" . " $r[2]</td>";
echo "<td alig='center' width='40'> $r[3]</td>";
echo "<td align='center' width='200'>$r[4]</td>";
echo "<td width='100' align='center'>$r[5]</td>";
echo "</tr>";
}
}
?>
</table>
</body>
</html>
your queries are wrong here
<?php
$sql = "SELECT id, manufacturer FROM coop";
$result = $conn->query($sql);
while ($r = mysqli_fetch_assoc($sql))
{
echo "<option value='$r[0]'> $r[0]</option>";
}
?>
this while ($r = mysqli_fetch_assoc($sql)) should be while ($r = mysqli_fetch_assoc($result ))
you should fetch your assoc with $result not with$sql
still you have some error bellow part

how to total all row in page that was sql LIMIT

i put limit in this code but how can i calculate the totalmin in this page?
here is my code
<?php
$id = $_GET['id'];
$sql = "SELECT * FROM time WHERE id = $id ORDER BY id DESC LIMIT 15";
$result = mysql_query($sql);
?>
<table border="0" style="width:50%">
<tr>
<th>Time in</th>
<th>Time Out</th>
</tr>
<?php
while($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td><center>".$row['totalmin']."</center></td>";
echo "</tr>";
}
</table>
mysql_close();
i want to know how to total all the mins in 15 row with php code
Something like this may do the trick, if you want to show sum only
But you should NOT use mysql_ functions, and this code is not sequre from SQL Injects
$id = $_GET['id'];
$sql = "SELECT * FROM time WHERE id = $id ORDER BY id DESC LIMIT 15";
$result = mysql_query($sql);
$sum = 0;
$num = 0;
?>
<table border="0" style="width:50%">
<tr>
<th>Time in</th>
<th>Time Out</th>
</tr>
<?php
while($row = mysql_fetch_array($result)){
$num++;
$sum += $row['totalmin'];
echo "<tr>";
echo "<td><center>".$row['totalmin']."</center></td>";
echo "</tr>";
if($num == 15){
echo "<tr>";
echo "<td><center>".$sum."</center></td>";
echo "</tr>";
}
}
?>
</table>
<?php
mysql_close();
You need to store $totalmin into your while loop and update it with new value if $row['totalmin'] is less then $totalmin.
Use something like this:
$totalmin = null;
while($row = mysql_fetch_array($result)) {
if (is_null($totalmin) || $row['totalmin'] < $totalmin) {
$totalmin = $row['totalmin'];
}
echo "<tr>";
echo "<td><center>" . $totalmin . "</center></td>";
echo "</tr>";
}
Sum up the minutes as you loop through the result:
$sum = 0;
while($row = mysql_fetch_array($result)){
$sum += floatval($row['totalmin']); //or intval()
echo "<tr>";
echo "<td><center>".$row['totalmin']."</center></td>";
echo "</tr>";
}
echo $sum;
make sure you close your while loop

How to filter while loop in PHP

I stuck on this,
I want to create a table with 10 rows, each row having "same drop down menu" of 10 products. But in every row different product selected. example in first row first product in second row second product and so on. The bellow code is showing drop down menu in each row but every row have first product selected. How can I select next product for next row?
<table>
<tr>
<td>
<select>
<?php
$query = "SELECT * FROM table_name WHERE featured=1 LIMIT 10";
$result = mysqli_query($connection, $query);
while($row = mysqli_fetch_array($result)){
echo "<option>".$row["product_name"]."</option>"
}
?>
</select>
</td>
<td>
<select>
<?php
$query = "SELECT * FROM table_name WHERE featured=1 LIMIT 10";
$result = mysqli_query($connection, $query);
while($row = mysqli_fetch_array($result)){
echo "<option>".$row["product_name"]."</option>"
}
?>
</select>
</td>
</tr>
</table>
<?
//Only Once call to DB
$query = "SELECT * FROM tbl WHERE featured=1 LIMIT 10";
$result = mysqli_query($connection, $query);
$pos = 0;
$currentRow = 0;
?>
<table>
<tr>
</td>
<select>
<?php
while($row = mysqli_fetch_array($result)){
if($pos==$currentRow)
echo "<option selected=\"selected\">".$row["product_name"]." </option>";
else
echo "<option>".$row["product_name"]."</option>";
$pos++;
}
mysqli_data_seek( $result, 0 );
$pos = 0;
$currentRow++;
?>
</select>
</td>
</tr>
<tr>
</td>
<select>
<?php
while($row = mysqli_fetch_array($result)){
if($pos==$currentRow)
echo "<option selected=\"selected\">".$row["product_name"]." </option>";
else
echo "<option>".$row["product_name"]."</option>";
$pos++;
}
mysqli_data_seek( $result, 0 );
$pos = 0;
$currentRow++;
?>
</select>
</td>
</tr>
</table>
your second while loop
$i=0;
while($row = mysqli_fetch_array($result)){
$i++;
if($i==2)
echo "<option selected=\"selected\">".$row["product_name"]."</option>";
else
echo "<option>".$row["product_name"]."</option>";
}
?>

Having a lot of trouble not uselessly accessing my DB

<?php
session_start();
include '../dbconnect_form_fields.php';
$res = mysql_query("SELECT * FROM form_fields") or die(mysql_error());
echo "<form id='list' action='form_calc.php' method='post'>
<table width='100%' border='1'>
<tr><td><select>";
while($row = mysql_fetch_assoc($res)){
echo "<option value='".$row['id']."'>".$row['field']." ".$row['price']."</option>";
}
echo "</select>
</td></tr>
<tr><td><select>";
$res1 = mysql_query("SELECT * FROM form_fields") or die(mysql_error());
while($row1 = mysql_fetch_assoc($res1)){
echo "<option value='".$row1['id']."'>".$row1['field']." ".$row1['price']."</option>";
}
?>
For some reason, when i change that line that says $res1 = mysql_query blah blah blah, It doesnt seem to work, the select field is empty with no options. It seems as though I would have to define $res as a mysql_fetch and for a second select box, access the DB a second time but using a different variable...
How can I make the $res variable carry across the loops without having to access the DB so many times... I play to have six of these loops... Help me gurus!
I think you might need to clarify what your intentions are. From your code you are creating two of the same select elements. If you want to output the same select element twice, you could try
$res = mysql_query("SELECT * FROM form_fields") or die(mysql_error());
echo "<form id='list' action='form_calc.php' method='post'>
<table width='100%' border='1'>
<tr>
<?php for($i=0;$i<1;$i++) { //Loop twice to create a second select element ?>
<td><select>";
while($row = mysql_fetch_assoc($res)){
echo "<option value='".$row['id']."'>".$row['field']." ".$row['price']."</option>";
}
echo "</select>
</td>
<?php } //End of for loop ?>
</tr>
Alternatively, you could just copy/paste the same while loop twice without changing any variables
<form id='list' action='form_calc.php' method='post'>
<table width='100%' border='1'>
<tr>
<td><select>
<?php
while($row = mysql_fetch_assoc($res)){
echo "<option value='".$row['id']."'>".$row['field']." ".$row['price']."</option>";
}
?>
</select>
</td>
<td><select>
<?php
while($row = mysql_fetch_assoc($res)){
echo "<option value='".$row['id']."'>".$row['field']." ".$row['price']."</option>";
}
?>
</select>
</td>
</tr>
</table>

Categories