How to avoid blank first result when searching from databse - php

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
?>

Related

PHP/HTML Button works only in the first element from a loop

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");
}

click dynamic php project title links to get full project details on profile page in php

please help. I am building a website where people can upload their projects. I designed it in such a way that one person can have multiple projects. I am fairly new to php and I know I posted way too much code but I need help
I have been able to make the project titles for each user show dynamically when they login in. The problem I have is how do I make each link load up the page with the full project details. Right now it only shows the first project details no matter what title is clicked. Below is the profile page.
<?php if(isset($login_projectTittle)){
echo "Click to see your projects";
?>
<br><br><br><br><br>
<?php
$query = "SELECT * FROM fullprojectdetails WHERE user_id=$login_id";
if ($result=mysqli_query($connection,$query)){
if($count = mysqli_num_rows($result)){
/* because of the mysqli_fetch_object function the variables have to be $rowe->title (as an object) */
while($rowe = mysqli_fetch_object($result)){
?>
<?php $_SESSION['projectstuff'] = $rowe ->projecttitle; ?>
<?php $_SESSION['projectIdNew'] = $rowe ->projectid; ?>
<?php echo $rowe ->projecttitle; ?>
<br>
<?php
}
/* mysqli_free_result frees p the result in the variable ths allowing for a new one each time */
mysqli_free_result($result);
}
}
}
else {echo "";}
?>
I used POST to send the data to the database.
The page that should load up the full project details is the project page. Contents to be displayed on this page are drawn from the session code which is included in the project page and displayed below
<?php
// Establishing Connection with Server by passing server_name, user_id and password as a parameter
require('databaseConnect.php');
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Selecting Database
require('databaseSelect.php');
session_start();// Starting Session
// Storing Session
$user_check=$_SESSION['login_user'];
// SQL Query To Fetch Complete Information Of User (MYSQL Inner Join used here) took hours to learn
$ses_sql=mysqli_query($connection, "select user.user_emailaddress, user.user_fullname, user.user_id, fullprojectdetails.fileToUpload, fullprojectdetails.projecttitle,
fullprojectdetails.projectcreated, fullprojectdetails.projectcategory, fullprojectdetails.projectcountry, fullprojectdetails.projectdetails,
fullprojectdetails.fundTime, fullprojectdetails.fundinggoal, fullprojectdetails.fileToUpload2, fullprojectdetails.name, fullprojectdetails.biography,
fullprojectdetails.yourlocation, fullprojectdetails.website, fullprojectdetails.fileToUpload3, fullprojectdetails.projectdetails2, fullprojectdetails.accountName, fullprojectdetails.bankName, fullprojectdetails.accountType,
fullprojectdetails.accountNo,fullprojectdetails.user_id, fullprojectdetails.projectid from user inner join fullprojectdetails on user.user_id=fullprojectdetails.user_id where user.user_emailaddress='$user_check'");
//saving variables to be used in every page with session_new included
$row = mysqli_fetch_assoc($ses_sql);
$login_session =$row['user_emailaddress'];
$login_fullname =$row['user_fullname'];
$login_id =$row['user_id'];
$login_projectTittle = $row['projecttitle'];
$login_projectLocation = $row['yourlocation'];
$login_projectCategory = $row['projectcategory'];
$login_projectFundGoal = $row['fundinggoal'];
$login_projectSummary = $row['projectdetails'];
$login_projectWebsite = $row['website'];
$login_projectVideo = $row['fileToUpload3'];
$login_Pic = $row['fileToUpload2'];
$login_projectID = $row['projectid'];
$login_projectPic = $row['fileToUpload'];
$login_projectPic = $row['fileToUpload'];
$login_projectPic = $row['fileToUpload'];
$login_projectPic = $row['fileToUpload'];
$login_projectPic = $row['fileToUpload'];
$login_projectPic = $row['fileToUpload'];
$login_projectPic = $row['fileToUpload'];
$login_projectPic = $row['fileToUpload'];
$login_projectFullDet = $row['projectdetails2'];
if(!isset($login_session)){
mysqli_close($connection); // Closing Connection
header('Location: login.php'); // Redirecting To Home Page
}
?>
Below is the project page code
<?php
include('session_new.php');
/*echo $_SESSION['projectstuff'];
if (isset($_POST['profile_btn'])){*/
/* if (isset ($_SESSION['projectstuff'])){
$ses_sql=mysqli_query($connection, "select * from fullproject where projecttitle='{$_SESSION['projectstuff']}");*/
?>
<!DOCTYPE html>
<html>
<body>
<!--require for the nav var-->
<?php require 'logged-in-nav-bar.php';?>
<div id= "upperbodyproject">
<div id= "projectheader">
<h2> <?php echo $login_projectTittle; ?><h2>
</div>
<!-- video and funder div-->
<div id = "vidFundCont">
<div id = "video">
<video width='100%' height='100%' controls> <source src="<?php echo $login_projectVideo ?>" type='video/mp4'>Your browser does not support the video tag.</source></video>
</div>
<div id = "funders">
<p> <strong> 20 </strong> <br> <br><span>funders </span> </p> <br> <br> <br>
<p> </p><br>
<p> <span>funded of N<?php echo $login_projectFundGoal ; ?> </span><p><br> <br> <br>
<p><strong> 15 </strong> <br> <span> <br> days left </span></p><br> <br> <br>
<button id = "projectPageButton"> Fund This Project </button>
</div>
<div id = "clearer"></div>
</div>
<!-- location and project condition -->
<div id = "location">
<p> Location: <?php echo $login_projectLocation ; ?> Category:<?php echo $login_projectCategory ; ?> </p>
</div>
<div id ="projectconditions">
<p> This project will only be funded if N<?php echo $login_projectFundGoal ; ?> is funded before the project deadline</p>
</div>
<div id = "clearer"> </div>
<!--project summary and profile -->
<div id = "nutshell">
<p><?php echo $login_projectSummary ; ?> </p> <br> <br>
<span>Share:</span> &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp
</div>
<div id = "projProfile">
<div id = "projProfileTop">
<img src="<?php echo $login_Pic ; ?>" width = "50%" height = "100%">
</div>
<div id ="projProfileBottom">
<br>
<p id = "profileNameStyle"> Name: <?php echo $login_fullname; ?> </p> <br>
<p> Website: <?php echo $login_projectWebsite ; ?> </p> <br>
<p> Email: <?php echo $login_session; ?> </p>
</div>
</div>
<div id = "clearer"> </div>
<!-- about and reward title -->
<div id = "aboutProjH">
<h2> About This Project </h2>
</div>
<div id = "rewardH">
<h2> Rewards For Supporting This Project</h2>
</div>
<div id = "clearer"> </div>
<!--project pic and dynamic rewards-->
<div id = "projPic">
<img src="<?php echo $login_projectPic;?>" width = "100%" height = "100%">
</div>
<div id = "rewardDet">
<br>
<span><?php echo $login_projectFullDet; ?> </span>
</div>
<div id = "clearer"> </div>
<div id = "clearer"> </div>
<!-- project details and empty divs -->
<?php
// code to select content form database and display dynamically in a div
$query = "SELECT * FROM reward WHERE user_id=$login_id";
if ($result=mysqli_query($connection,$query)){
if($count = mysqli_num_rows($result)){
/* because of the mysqli_fetch_object function the variables have to be $rowe->title (as an object) */
while($rowe = mysqli_fetch_object($result)){
?>
<div id = "projDet">
<p> <h2><?php echo "N".$rowe->pledgeAmount. " " . "or more"; ?></h2></p> <br>
<p><span> <br><?php echo $rowe ->title; ?></span></p> <br>
<p> <span><?php echo $rowe ->description; ?></span></p> <br>
<p> <span>Estimated Delivery Time: <br> <?php echo $rowe ->deliverytime . " ". $rowe ->deliveryyear; ?></span></p> <br>
<p> <span><?php echo $rowe ->shippingdetails; ?></p></span> <br>
<p> <span>Number of Rewards Available: <br><?php echo $rowe ->reward1No; ?></span></p>
</div>
<!--<div id = "bsideProjDet">
</div> -->
<div id = "clearer"> </div>
<?php
}
/* mysqli_free_result frees p the result in the variable ths allowing for a new one each time */
mysqli_free_result($result);
}
}
/* display dynamic form */
/*$s_sql=mysqli_query($connection, "SELECT * FROM reward WHERE user_id=$login_id");
$rowe = mysqli_fetch_assoc($s_sql);
?>
<?php echo $rowe['title']; ?><br><br>
<?php echo $rowe['pledgeAmount']; ?><br><br>
<?php echo $rowe['description']; ?><br><br>
<?php echo $rowe['deliverytime']; ?><br><br>
<?php echo $rowe['deliveryyear']; ?><br><br>
<?php echo $rowe['shippingdetails']; ?><br><br>
<?php echo $rowe['reward1No']; ?><br><br> */
?>
<div id = "reportProj">
<button id = "projectPageButton"> Report This Project To Gbedion</button>
</div>
<!--<div id = "bsideReportProj"> </div>-->
<div id = "clearer"> </div>
<!-- </div> -->
</div>
<?php
/*}
}
else {
echo "Select a project";
}*/
?>
<!-- page footer-->
<?php require 'logged-in-footer.php';?>
</body>
</html>
The project Id is the primary key and user id is the foreign key in fullprojectdetails table. If more database details are needed please let me know. Had to abandon this project for a month because of this problem.
First don't store the projectid and project title on a session variable, on the profile page, just create a normal variable and store them on that variable, then when you linking to project.php create a query strings, with the id of the project then with the title of the project, ie Dynamic Project Title I believe when you store the id's of the projects in a session, once you click on one project, the browser will always use that id no matter which project you click next, until you unset/destroy the sessions(logout).
there fore this on profile :
<?php $_SESSION['projectstuff'] = $rowe ->projecttitle;?>
<?php $_SESSION['projectIdNew'] = $rowe ->projectid; ?>
<?php echo $rowe ->projecttitle; ?>
should change to :
<?php $projectstuff = $rowe ->projecttitle; ?>
<?php $projectIdNew = $rowe ->projectid; ?>
<?php echo $rowe ->projecttitle; ?>
Then on projetc.php
you first check if the id isset and also the title, then do your queries
this is how you would do it
project.php
<?php
session_start();
//validate your sessions
if (isset($_GET['projectid']) && isset($_GET['projecttitle'])) {
$currentProjectID = urldecode(base64_decode($_GET['projectid']));
$projectTitle = $_GET['projecttitle'];
?>
<?php
require 'logged-in-nav-bar.php';
?>
<div id= "upperbodyproject">
<div id= "projectheader">
<h2> <?php echo $projectTitle; //from the query string ?></h2>
</div>
<!-- get the details of this project -->
<?php
$query = "SELECT * FROM fullprojectdetails WHERE projectid=$currentProjectID";
// Continue your staff then
?>
</div><!-- upperbodyproject -->
<?php
} else {
echo "could not find project id";
}
?>
Hope this will help point you to the correct direction, I would suggest that you look at mysqli prepared statements, and also at PDO prepared statements.

Sending multiple values from php to ajax

I am trying to send 2 values from different parts of a page to the same ajax function. Basically, it's a search filter wherein selecting categories is one module and selecting brands is another both of which are attributes of a product and independent of each other.
I need 3 conditions where search is performed either
only if brand is selected or
only if cat is selected or
if both are selected.
My issue is 1 and 3 are working. but 2nd condition isn't as it takes the cat value as the brand. Please help me correct my function.
My AJAX function:
function filter(subid) {
alert(subid);
var brand_filter = new Array();
$("input:checked").each(function() {
brand_filter.push($(this).val());
});
$.ajax({
type: 'POST',
url: 'getSubcategoryProducts.php',
data: {
brand_filter:brand_filter,
subId: subid
},
success: function(data) {
//alert(data);
$('#productDetail').html(data).fadeIn('slow');
$('#sub_cat_menuItem a').addClass('.collection-category-brands li.active, .collection-category-brands li:hover');
}
});
}
Module of selecting brand:
<div class="collection-brands-filter">
<h4>Filter by Brands</h4>
<div class="brand-filter-form">
<form>
<?php $brandlist = $objBrands-> getAll("status=1");
foreach($brandlist as $branditem){
?>
<span><input type="checkbox" name="brand_filter[]" <?php if ('checked="checked"'){ ?> onClick="filter(<?php echo $branditem['id'];?>);" <?php } ?> value="<?php echo $branditem['id'];?>"/><?php echo $branditem['name'];?></span>
<?php } ?>
</form>
</div>
</div>
Module of selecting category:
<div class="collection-category-brands">
<ul>
<?php
//Fetching sub categories of the main_category
while($query2res = mysql_fetch_array($query2)){ ?>
<li id="sub_cat_menuItem"><?php echo $query2res['1'];?></li>
<?php $subId = $query2res['id'];}?>
</ul>
</div>
getSubcategoryProducts.php code:
<?php
include_once("class/site_root.php");
include('includes/connect.php');
include_once(DIR_ROOT."class/functions.php");
include_once(DIR_ROOT."class/products.php");
$objProducts = new products();
$subId = $_POST['subId'];
//IF ONLY BRAND IS SELECTED AND NOT SUB CATEGORY
if(isset($_POST['brand_filter'])&&!isset($_POST['subId']))
{
echo "only brand is selected";
foreach($_POST['brand_filter'] as $item)
{
$res = $objProducts->getAll('status=1 and brand_id='.$item);?>
<ul>
<?php foreach($res as $val){?>
<li>
<img src="<?php echo $val['img_small']; ?>" alt="icon"/>
<div class="collection-item-hover">
<div class="collection-category-detail">
Know More
<div class="collection-item-detail">
<h4><?php echo $val['product_name']; ?></h4>
<p><?php echo $val['descr_small']; ?></p>
</div>
</div>
</div>
</li>
<?php }?>
</ul>
<?php }
}
//IF ONLY SUB CATEGORY IS CHOSEN!
else if(isset($subId)&&!isset($_POST['brand_filter'])){
echo "only sub cat selected!";
$productsquery=mysql_query("SELECT products.product_name,products.id,products.main_id,
products.img_small,products.descr_small,sub_category.name
FROM products
LEFT JOIN sub_category on
products.sub_id=sub_category.id
WHERE sub_category.id=".$subId);
$count = mysql_num_rows($productsquery);
$subNameVal=mysql_fetch_array(mysql_query("select name from sub_category where id=".$subId));
?>
<h3><?php echo $subNameVal['name'];?></h3>
<?php
if($count>0)
{
?>
<ul>
<?php
while($productsRes=mysql_fetch_array($productsquery))
{
?>
<li>
<img src="<?php echo $productsRes['img_small']; ?>" alt="icon"/>
<div class="collection-item-hover">
<div class="collection-category-detail">
Know More
<div class="collection-item-detail">
<h4><?php echo $productsRes['product_name']; ?></h4>
<p><?php echo $productsRes['descr_small']; ?></p>
</div>
</div>
</div>
</li>
<?php }
?>
</ul>
<?php }
}
//IF BOTH ARE SELECTED
else //if(isset($_POST['brand_filter'])&&isset($subId))
{
echo "both selected!";
echo
"SUB-".$subId."<br/>";
foreach ($_POST['brand_filter'] as $item)
{
echo "brand-".$item."<br/>";
}
}
?>

mysql query ironing one search result on webpage?

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
}

php table with hyperlink - record issue

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>

Categories