I have two tables listed (on screen) in PHP and the left should be hyperlinked so when click on it the right table will show a query. So at the beginning it should be empty then when clicked refresh the page with the selected listname's result.
unfortunately I have no experience with these things so i don't know the concept of it yet, but I am happy to learn:)
<form method="post" action="test.php">
<div id="left"><table>
<?php
$left="SELECT * FROM groups";
$resultleft=mysql_query($left);
while ($resultleft=mysql_query($left)) {
echo "<tr><td>".$left['id'].'</td><td>'.$left['listname']."</td></tr>";
}
?>
</table></div>
<div id="right"><table>
<?php
$right="SELECT * FROM grouplink WHERE grouplink.group_id= ";
$resultright=mysql_query($right);
while ($resultright=mysql_query($right)) {
echo "<tr><td>'.$right['people_name']."</td></tr>";
}
?>
</table></div>
<?php
if (isset($_POST('???'))){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=test.php\">";
}
?>
</form>
any help would be appreciated
Can for example link to table.php?gid=n, where n would be the group id. You can then check if $_GET['gid'] isset, and if it is, take that id and put it in your query.
if(isset($_GET['gid']))
$right = sprintf("SELECT * FROM grouplink WHERE grouplink.group_id=%u", $_GET['gid']);
You need mysql_fetch_assoc
instead of mysql_query
in:
while ($resultleft=mysql_query($left)) {
echo "<tr><td>".$left['id'].'</td><td>'.$left['listname']."</td></tr>";
}
and also in:
$resultright=mysql_query($right);
while ($resultright=mysql_query($right)) {
echo "<tr><td>'.$right['people_name']."</td></tr>";
so this will be:
while ($left=mysql_fetch_assoc($resultleft)) {
echo "<tr><td>".$left['id'].'</td><td>'.$left['listname']."</td></tr>";
}
and similar issue with right
Thanks to Svish here is my new working code:
<?php include("db_con1.php");?>
<html>
<head>
</head>
<body>
<form method="post" action="test.php">
<div id="left">
<?php
$queryl = $pdo->prepare('SELECT id, name FROM test1 ORDER BY name ASC');
$queryl->execute();
?>
<ul>
<?php foreach ($queryl as $i => $rowl) { ?>
<li>
<?php if ($i)?>
<input name="checkbox_add[]" id="test_<?php echo $i ?>" type="checkbox" value="<? echo $rowl['id']; ?>"/>
<label for="test_<?php echo $i ?>"><?php echo $rowl['name']; ?></label>
</li>
<?php } ?>
</ul>
</div>
<div id="right">
<?php
if(isset($_GET['gid'])) {
$gid=$_GET['gid'];
$queryr = $pdo->prepare('SELECT test3.name FROM test1, test2, test3 WHERE test1.id=test2.groupid AND test3.id=test2.peopleid AND test1.id='.$gid.' ORDER BY test3.name ASC');
$queryr->execute();
}
?>
<ul>
<?php foreach ($queryr as $i => $rowr) { ?>
<li>
<?php if ($i)?>
<input name="checkbox_del[]" id="test_<?php echo $i ?>" type="checkbox" value="<? echo $rowr['id']; ?>"/>
<label for="test_<?php echo $i ?>"><?php echo $rowr['name']; ?></label>
</li>
<?php } ?>
</ul>
</div>
</form>
</body>
</html>
Related
I am learning PHP and am stuck right now. In a while loop, all the to-do items are displayed. Each of these elements has a delete button and a check button. The delete button works in every element, but the check button only works in the first one. The check button is to be used to check off an element. When the check button is pressed, an error message is displayed that this element could not be deleted. Only in the first element of course not. I can't find the error.
I suspect there is something wrong with the in the index
<?php
$todos = $conn->query("SELECT * FROM todos ORDER BY id DESC");
?>
<div class="show-todo-section">
<?php if($todos->rowCount() <= 0){ ?>
<div class="todo-item">
<div class="empty">
<img src="img/f.png" width="100%" />
<img src="img/Ellipsis.gif" width="80px">
</div>
</div>
<?php } ?>
<?php while($todo = $todos->fetch(PDO::FETCH_ASSOC)) { ?>
<div class="todo-item">
<form action="app/checked.php" method="POST">
<button class="check-box" value="<?php echo $todo['id']; ?>" name='checked' type="submit">Check</button>
</form>
<form action="app/delete.php" method="POST">
<button class="remove-to-do" value="<?php echo $todo['id']; ?>" name='delete' type="submit">x</button>
</from>
<?php if($todo['checked']){ ?>
<h2 class="checked"><?php echo $todo['title'] ?></h2>
<?php }else { ?>
<h2><?php echo $todo['title'] ?></h2>
<?php } ?>
<small>created: <?php echo $todo['date_time'] ?></small>
<?php for ($i = 1; $i <= $todo['coins']; $i++) {
echo "<img class='coins' src='img/coin.gif' width='40px' />";
} ?>
</div>
<?php } ?>
</div>
the checked.php
<?php
if (isset($_POST['checked'])) {
require '../db_conn.php';
$id = $_POST['checked'];
if(empty($id)){
header("Location: ../index.php?mess=errorChecked". $id);
}else{
$todos = $conn->prepare("SELECT id, checked FROM todos WHERE id=?");
$todos->execute([$id]);
$todo = $todos->fetch();
$uId = $todo['id'];
$checked = $todo['checked'];
$uChecked = $checked ? 0 : 1;
$res = $conn->query("UPDATE todos SET checked=$uChecked WHERE id=$uId");
header("Location: ../index.php?mess=successChecked");
$conn = null;
exit();
}
} else{
header("Location: ../index.php?mess=errorChecked");
}
I have search webpage, but when the search is run, there is a blank first result.
<?php include "headnav.php";
$count = 0;
$sql = "SELECT * FROM lost_property";
if (!empty($_POST)) {
$name = mysqli_real_escape_string($dbconnect, htmlspecialchars($_POST['name']));
$item = mysqli_real_escape_string($dbconnect, htmlspecialchars($_POST['item']));
$area = mysqli_real_escape_string($dbconnect, htmlspecialchars($_POST['area']));
$sql = "
SELECT *
FROM lost_property
JOIN item
ON lost_property.itemID = item.itemID
JOIN area
ON lost_property.areaID = area.areaID
WHERE name LIKE '%$name%'
AND item LIKE '%$item%'
AND area LIKE '%$area%'
ORDER
BY lost_property.name ASC
";
$search_query = mysqli_query($dbconnect, $sql);
$count = mysqli_num_rows($search_query);
}
$result = $dbconnect->query($sql);
?>
<body>
<div class="form">
<h1>Search for lost property here:</h1>
<form action="" method="POST">
Name:
<input type="text" placeholder="Name" name="name">
Item type:
<select name="item" class="dropdown">
<option value="" disabled selected>Item</option>
<?php
$item_sql = "SELECT DISTINCT item FROM `lost_property`
JOIN item ON (lost_property.itemID = item.itemID)
ORDER BY item ASC
";
$item_query = mysqli_query($dbconnect, $item_sql);
$item_rs = mysqli_fetch_assoc($item_query);
do {
?>
<option value="<?php echo $item_rs['item']; ?>"><?php echo $item_rs['item']; ?></option>
<?php
} while ($item_rs = mysqli_fetch_assoc($item_query));
?>
</select>
Area (where it was found):
<select name="area" class="dropdown">
<option value="" disabled selected>Area</option>
<?php
$area_sql = "SELECT DISTINCT area FROM `lost_property`
JOIN area ON (lost_property.areaID = area.areaID)
ORDER BY area ASC
";
$area_query = mysqli_query($dbconnect, $area_sql);
$area_rs = mysqli_fetch_assoc($area_query);
do {
?>
<option value="<?php echo $area_rs['area']; ?>"><?php echo $area_rs['area']; ?></option>
<?php
} while ($area_rs = mysqli_fetch_assoc($area_query));
?>
</select>
<input type="submit" value="Search" name="btn">
</form>
</div>
<div class="gallery">
<h2>Search results:</h2>
<?php
//check for results. If there are none display error
if ($count < 1) {
?>
<div class="error">
<h1>No results were found.</h1>
</div>
<?php
} //end if
else {
do {
?>
<!-- display image and information from database and show in gallery -->
<div class="results">
<h3><?php echo $search_rs['name']; ?></h3>
<h3><?php echo $search_rs['item']; ?></h3>
<p><?php echo $search_rs['area']; ?></p>
</div>
<?php
} // end of do
while ($search_rs = mysqli_fetch_assoc($search_query));
} //end else
//if there are any display
?>
</div>
</table>
</body>
</html>
It's hard to see without any CSS, but no matter what is searched, there is always one result, but the h3 and p fields don't have any content. If there wasn't the no results error message it would pop up there too. What is causing this first result?
Use while (){}, if you use do instead it will run once first (credit to Magnus Eriksson).
It ends up like
else {
while ($search_rs = mysqli_fetch_assoc($search_query)) {
?>
<!-- display image and information from database and show in gallery -->
<div class="results">
<h3><?php echo $search_rs['name']; ?></h3>
<h3><?php echo $search_rs['item']; ?></h3>
<p><?php echo $search_rs['area']; ?></p>
</div>
<?php
} // end of do
} //end else
//if there are any display
?>
instead of
else {
do {
?>
<!-- display image and information from database and show in gallery -->
<div class="results">
<h3><?php echo $search_rs['name']; ?></h3>
<h3><?php echo $search_rs['item']; ?></h3>
<p><?php echo $search_rs['area']; ?></p>
</div>
<?php
} // end of do
while ($search_rs = mysqli_fetch_assoc($search_query));
} //end else
//if there are any display
?>
I tried my best but I stuck on this problem. Sorry for my bad english.
So here is the situation:
I am trying to make a little shopping site. My database table looks like this:
id pic titel desc price
1 gta5.png Grand Theft Auto 5 A open world game... 49.99
2 cod.png Call Of Duty: MW A Ego-Shooter game... 59.99
3 play4.png Playstation 4 Next-Gen Console... 249.99
4 contr.png Ps4 Controller Next-Gen Equipment... 69.99
On the Database Class file I have some function where I get the column information from the table:
public function getImg(){
while($row = mysqli_fetch_row($this->query)){
return $row[1];
}
}
public function getTitel(){
while($row = mysqli_fetch_row($this->query)){
return $row[2];
}
}
...
With getImg() I will get every information on the pic column like: gta5.png, cod.png etc.
My problem is starting here. I want to print out every column with a for loop on the index.php file. But I can't see any information. Here is the index.php file:
<ul>
<li>
<div class="container">
<div class="content">
<?php
for($i = 0; $i < $database->getTableLength(); $i++){
?>
<img src="image/<?php echo $database->getImg(); ?>"></img>
<h4><?php echo $database->getTitel(); ?></h4>
<h5><?php echo $database->getDesc(); ?></h5>
<h2><?php echo $database->getPrice(); ?>€</h2>
<input type="button" name="submit" value="Buy">
<?php
}
?>
</div>
</div>
</li>
</ul>
But as I said there is nothing. The getTableLength() function is the length of the table (In this case 4). I also tried to print it out one by one like this:
<ul>
<li>
<div class="container">
<div class="content">
<?php
for($i = 0; $i < $database->getTableLength(); $i++){
echo $database->getImg() . "<br>";
}
?>
</div>
</div>
</li>
</ul>
But again failed.. I can see even the br tag but not the information that I want to print out. When I put the echo line before the for loop, then I can see the first index of the column (gta5.png), but I want to have all columns.
Hope to see some solutions. Thanks for any kind of help!
EDIT:
It is working when I write 5 instead of the length function:
for($i = 0; $i < 5; $i++){
echo $database->getImg();
}
But I still want to use that function. The getTableLength() function isn't working as I expected. There is nothing to see when I insert the function. The getTable function looks like this:
public function getTableLength(){
$sql = "SELECT COUNT(*) AS num FROM `$this->table`";
$this->query = mysqli_query($this->connect, $sql);
if($this->query){
$row = mysqli_fetch_assoc($this->query);
return $row['num'];
}
}
When I call it on the index.php file like:
$database->getTableLength();
I can see nothing and everything is gone. It's like buggy.
Fixed. It was to complicated. Think simple and do it better..
Here is the solution:
<?php
$table = $database->getTableName();
$sql = "SELECT * FROM `$table`";
$connect = $database->getConn();
$result = mysqli_query($connect, $sql);
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_array($result)){
?>
<form method="post" action="index.php?action=add&id=<?php echo $row["id"] ?>">
<ul>
<li>
<div class="container">
<div class="content">
<img src="image/<?php echo $row["bild"]; ?>"></img>
<h4><?php echo $row["titel"]; ?></h4>
<h5><?php echo $row["beschreibung"]; ?></h5>
<h2><?php echo $row["preis"]; ?>€</h2>
<input type="hidden" name="hidden_name" value="<?php echo $row["titel"]; ?>">
<input type="hidden" name="hidden_price" value="<?php echo $row["preis"]; ?>">
<input type="submit" name="add_to_cart" value="Kaufen">
</div>
</div>
</li>
</ul>
<?php
I am trying to generate Tabs from 1st while loop and within that table from second while loop.
I will fetch the records date wise from my 1st table ie, treatment from that i am generating Tab, In another table called treatment_litems i have stored all the line items for treatment table records. So for 1st date (Tab) from treatment table, i want to display all the related records from treatment_litems in table format.
I am getting records but Tabs are not getting added, but everytime new Tabs are generating.
here is my code
<ul class="nav nav-tabs">
<?php $i=1; while($tt2 = mysqli_fetch_array($tt1)) { ?>
<li>
<?php echo $tt2['date']; ?>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane fade active in" id="tab_1_<?php echo $i; ?>">
<?php $l1 = mysqli_query($con, "SELECT * FROM treatment_litems WHERE tid=".$tt2['tid'].""); ?>
<table class="table">
<thead><tr><th>Drugs</th><th>Route</th><th>Dosage</th></tr></thead>
<tbody>
<?php
while($l2 = mysqli_fetch_array($l1)) { ?>
<tr><td><?php echo $l2['drugs']; ?></td>
<td><?php echo $l2['route']; ?></td>
<td><?php echo $l2['dosage']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
<?php $i++ ; } ?>
</div>
Here is the image
**** EDITED ******
<ul class="nav nav-tabs">
<?php
while($tt2 = mysqli_fetch_array($tt1)) { ?>
<li>
<?php echo $tt2['date']; ?>
</li>
</ul>
<?php } ?>
<div class="tab-content">
<div class="tab-pane fade active in" id="tab_1_<?php echo $tt2['tid']; ?>">
This is my working code, you can get hints by this:
<div id="tabs" style="float:left">
<ul>
<?php $getlang=mysql_query("select * from language where Mid='$mid' and status='1' order by LanguageName asc");
if(mysql_num_rows($getlang)>0){
while($arr=mysql_fetch_array($getlang)){?>
<li><?php echo $arr['LanguageName']; ?></li>
<?php }}?>
</ul>
<?php $getlang=mysql_query("select * from language where Mid='$mid' and status='1' order by LanguageName asc");
if(mysql_num_rows($getlang)>0){
while($arr=mysql_fetch_array($getlang)){?>
<div id="tabs-<?php echo $arr['LanguageID'] ?>">
<input type="hidden" name="LanguageId[]" class="langID" value="<?php echo $arr['LanguageID']; ?>">
<input type="hidden" name="mid" value="<?php echo $mid; ?>">
<div class="rows">
<label>Notification Text</label><br/>
<textarea rows="3" cols="10" name="description[]" id="" maxlength="1000" style="width:700px; height:100px;" required="required"></textarea>
</div>
</div>
<?php } }
Check and do let me know.
My query printed on webpage
SELECT * FROM products WHERE (product_name like '%meat%' OR description like '%meat%' OR ingradients like '%meat%') AND hide!=1 ORDER BY id ASC
If I run the same query query in mysql its showing 2 results with my php loop code its showing only one result,
my php code
<?php
$keyword=mysql_real_escape_string($_GET['Keyword']);
$query2 = "SELECT * FROM products WHERE (product_name like '%$keyword%' OR description like '%$keyword%' OR ingradients like '%$keyword%') AND hide!=1 ORDER BY id ASC ";
echo $query2 ;
$result2 = mysql_query($query2) or die('Error, query failed2');
if (mysql_num_rows($result2)>0){
mysql_data_seek($result2, 0);
$row2 = mysql_fetch_array($result2, MYSQL_ASSOC)
?>
<ul id="product-listing">
<?php
$i=1;
while($row2 = mysql_fetch_array($result2, MYSQL_ASSOC)){ ?>
<li <?php $i; if ($i%3==0) {echo "class=\"last\"";} ?>>
<div class="img">
<?php if ( $row2['new'] ==1 ) { ?>
<div class="new"><img src="images/new.png" width="18" height="41" /></div>
<?php } ?>
<img src="images/products/284X190/<?php echo $row2['image_1']; ?>" width="284" height="190" alt="" title="" /> </div>
<div class="name"><?php echo $row2['product_name']; ?></div>
<form action="" method="post">
<div class="price">Price:
<?php if ( $row2['market_price'] !=0 ) { ?>
<span> $<?php echo $row2['market_price']; ?> </span>
<?php } ?>
$<?php echo $row2['price']; ?></div>
<div class="add-to-cart">
<input type="image" src="images/btn-1.jpg" />
</div>
<div class="clear"></div>
</form>
</li>
<?php $i++; } ?>
</ul>
<?php } else { ?>
No Products,
<?php } ?>
You should use a while loop in your php.
while($item = mysql_fetch_assoc($query))
{
print_r($item); // echo out whatever you need to for each item returned
}