I have a table of contacts and a search form the returns the contact searched for. All work fine as it should. But is it possible to show an instance of the first row only in a separate div or span at top of page? I have seen this done in MS Access, is it possible in php? Here is my search query but I have no idea how to echo or show the first row only into a title or heading div.
$sql = "SELECT * FROM people";
if( isset($_GET['search']) ){
$name = mysqli_real_escape_string($con, htmlspecialchars($_GET['search']));
$name2 = mysqli_real_escape_string($con, htmlspecialchars($_GET['search2']));
$sql = "SELECT * FROM people WHERE first_name ='$name' AND last_name= '$name2'";
}
$result = $con->query($sql);
?>
<?php
while($row = $result->fetch_assoc()){
?>
<tr>
<td><?php echo $row['people_id']; ?></td>
<td><?php echo $row['first_name']; ?></td>
<td><?php echo $row['last_name']; ?></td>
<td><?php echo $row['address']; ?></td>
<td><?php echo $row['contact']; ?></td>
</tr>
}
I just would do it like this (updated answer):
$tableData = [];
$divData = "";
while ($row = $result->fetch_assoc()) {
// empty tableData --> first row
if (empty($tableData)) {
// do your special for the first row here (div)
$divData = "hello world! ".$row['first_name']." ".$row['last_name'];
}
$tableData[] ="<tr>
<td>".$row['people_id']."</td>
<td>".$row['first_name']."</td>
<td>".$row['last_name']."</td>
<td>".$row['address']."</td>
<td>".$row['contact']."</td>
</tr>";
}
echo "<div>".$divData."</div>";
echo "<table>";
echo implode("\n", $tableData);
echo "</table>";
Related
I'm trying to display a table of dates, albums and reviews made by users. The date and review show up with no problem, but at the moment my query is only displaying the first CD Title in the list.
Table 'cdreview' has columns 'CDID', 'userID', 'reviewDate', 'reviewText'.
Table 'cd' has 'CDTitle', so I've used a natural JOIN to link the 2 tables by the CDID, but I can't display the correct CDTitle.
Any help would be extremely grateful.
<?php
require_once 'database_conn.php';
$userid = $_SESSION['userSession'];
$sql = "SELECT * FROM cdreview JOIN cd WHERE '$userid'=cdreview.userID ORDER BY reviewDate ASC";
$result = mysqli_query($conn, $sql) or die(mysqli_error($conn));
$row = mysqli_fetch_assoc($result);
$date = $row['reviewDate'];
$album = $row['CDTitle'];
$review = $row['reviewText'];
$cdid = $row['CDID'];
?>
<tr align='center'>
<td><?php echo $date;?></td>
<td><?php echo $album;?></td>
<td><?php echo $review;?></td>
<td><a href="edit_review.php?id=<?php echo $cdid;?>">Edit</td>
<td><a href="album.php?id=<?php echo $cdid;?>">Delete</td>
</tr>
</table>
You have to iterate through results :
while ($row = mysqli_fetch_assoc($result) ){
$date = $row['reviewDate'];
$album = $row['CDTitle'];
$review = $row['reviewText'];
$cdid = $row['CDID'];
// print stuff
}
mysqli_fetch_assoc is returning a result set.
You have to loop through this result set and handle each result separately.
while ($row = mysqli_fetch_assoc($result)) {
$date = $row['reviewDate'];
$album = $row['CDTitle'];
$review = $row['reviewText'];
$cdid = $row['CDID'];
?>
<tr align='center'>
<td><?php echo $date; ?></td>
<td><?php echo $album; ?></td>
<td><?php echo $review; ?></td>
<td><a href="edit_review.php?id=<?php echo $cdid; ?>">Edit</td>
<td><a href="album.php?id=<?php echo $cdid; ?>">Delete</td>
</tr>
<?php
}
Here's my code sir:
<?php
session_start();
include 'include/db_config.php';
$result = $dbs->prepare("SELECT * FROM service_info ORDER BY id DESC");
/*$result->bindParam(':id', $_SESSION['id']);*/
$result->execute();
for($i=0; $row = $result->fetch(); $i++){
$result2 = $dbs->prepare("SELECT *FROM customer_info ORDER BY id DESC");
$result2->execute();
for($j=0; $row2 = $result2->fetch();$j++){
?>
<tr class="record">
<td><?php echo $row2['firstname'];?></td>
<td><?php echo $row['no_guest']; ?></td>
<td><?php echo $row['type_service']; ?></td>
<td><?php echo $row['datepicker']; ?></td>
<td><?php echo $row['t_time']; ?></td>
<td> delete </td>
</tr>
<?php
}
}
?>
i didnt use a LEFT JOIN for displaying data's from 2 tables. i just want to do it in my own way . But my problem is it duplicates my data . before i inserted my second query its just 2 data's and now its 4 already. i just cant figure it out where is the duplication occurs.
Someone help me out please . Thanks in Advance.
You display data in second for, which is inside first for. So you get result count of customer_info table length*service_info length results. You should save info in arrays and then use only one for cycle. Eg.:
<?php
session_start();
include 'include/db_config.php';
$services = array();
$customers = array();
$result = $dbs->prepare("SELECT * FROM service_info ORDER BY id DESC");
/*$result->bindParam(':id', $_SESSION['id']);*/
$result->execute();
for($i=0; $row = $result->fetch(); $i++){
$services[] = $row;
}
$result2 = $dbs->prepare("SELECT *FROM customer_info ORDER BY id DESC");
$result2->execute();
for($j=0; $row2 = $result2->fetch();$j++){
$customers[] = $row2;
}
foreach($services as $key => $service) {
?>
<tr class="record">
<td><?php echo $customers[$key]['firstname'];?></td>
<td><?php echo $service['no_guest']; ?></td>
<td><?php echo $service['type_service']; ?></td>
<td><?php echo $service['datepicker']; ?></td>
<td><?php echo $service['t_time']; ?></td>
<td> delete </td>
</tr>
<?php
}
?>
Btw, i suggest you use different approach, like joins
i'm a beginner in php and i'm hoping that somebody here can help me in this.
i have a table that has a check box to select an item and a text box to indicate the quantity the person wants to borrow. i was wondering how i can retrieve both these data and then save them in my database.
here is a part of my code:
<td width="30">
<input id="optionsCheckbox" class="uniform_on" name="selector[]" type="checkbox" value="<?php echo $id; ?>">
</td>
<td><?php echo $row['item_code']; ?></td>
<td><?php echo $row['item_name']; ?></td>
<td align="center">
<img class="img-rounded" src="<?php echo $row['item_image'];?>" border="0" onMouseOver="showtrail('<?php echo $row['item_image'];?>','<?php echo $row['item_code'].": ".$row['item_name'];?> ',200,5)" onMouseOut="hidetrail()"></a></td>
<td><?php echo $row['item_quantity'] - $row['item_consumption']; ?> <?php echo $row['unit']; ?></td>
<td><input type="text" name="consume[]" pattern="[0-9]{1,4}"/></td>
here's what i have so far:
$id=$_POST['selector'];
$consume= $_POST['consume'];
$N = count($id);
for($i=0; $i < $N; $i++)
{
$query = $conn->query("select * from item where item_id ='$id[$i]'")or die(mysql_error());
$row = $query->fetch();
$code = $row['item_code'];
$name = $row['item_name'];
echo $code; echo $name;
echo $id;
}
$x = count($consume);
for($y=0; $y < $x; $y++)
{echo $consume;
}
the echos are just for checking if the data goes through. i'm just making sure they do before i make a query for table insertion.
what i'm trying to do is to post them on another page, display them in a table (a la shopping cart) and then make the borrower fill out a form with his details.
ok, using gul's answer below, this is what i did:
$id = $_POST['selector'];
$consume = $_POST['consume'];
//array var for getting the values
$ids = ''; $consumes='';
//loop the posted array
for($i=0;$i<count($id);$i++)
{
$ids .= $id[$i].',';
$consumes.="'".$consume[$i]."',";
}
//remove the last comma
$ids = substr($ids,0,-1);
$consumes = substr($consumes,0,-1);
$query = $conn->query("select * from item where item_id IN($ids)")or die(mysql_error());
//table element
echo "<table border='1' style='border-collapse:
collapse;border-color: silver;'>";
echo "<tr style='font-weight: bold;'>";
echo "<td width='150' align='center'>Item Code</td>";
echo "<td width='150' align='center'>Item Name</td>";
echo "<td width='150' align='center'>Quantity</td>";
echo "</tr>";
//get data in table
$row = $query->fetch();
$code = $row['item_code'];
$name = $row['item_name'];
$table ="";
$table.="<tr>";
$table.="<td>".$code."</td>";
$table.="<td>".$name."</td>";
$table.="<td>".$consumes."</td>";
$table.="</tr>";
$table.="</table>";
//echo the table
echo $table;
however, when i select multiple items, only the last one shows up, but the consumes show up like this:
consume shows in one cell with single quotes and comma. help?
Try like this:
//first get the post
$id = $_POST['selector'];
$consume = $_POST['consume'];
//array var for getting the values
$ids = ''; $consumes='';
//loop the posted array
for($i=0;$i<count($id);$i++)
{
$ids .= $id[$i].',';
//$consumes.="'".$consume[$i]."',";
}
//remove the last comma
$ids = substr($ids,0,-1);
$query = $conn->query("select * from item where item_id IN($ids))or die(mysql_error());
//table element
$table = "<table border='1'>";
$table.="<tr>";
$table.="<th>Code</th><th>Name</th>";
$table.="</tr>";
//get data in table
$row = $query->fetch();
$code = $row['item_code'];
$name = $row['item_name'];
$table.="<tr>";
$table.="<td>".$code."</td>";
$table.="<td>".$name."</td>";
$table.="</tr>";
$table.="</table>";
//echo the table
echo $table;
ok, so i tried this and it worked. thanks to gul for helping me.
$id = $_POST['selector'];
$consume = $_POST['consume'];
//array var for getting the values
$data = array();
//loop the posted array
for($i=0;$i<count($id);$i++)
{
$row = array('selector'=>$id[$i],'consume'=>$consume[$i]);
//push in array
array_push($data,$row);
$query = $conn->query("select * from item where item_id ='$id[$i]'")or die(mysql_error());
$row = $query->fetch();
$code = $row['item_code'];
$name = $row['item_name'];
while ($row = $query->fetch()) {
$id = $row['id'];
}
?>
<tr>
<td><?php echo $code; ?></td>
<td><?php echo $name; ?></td>
<td><?php echo $consume[$i]; ?></td>
<td></td>
</tr>
<?php } ?>
</tbody>
</table>
I'm trying to make an page where all the things in the table in the database are displayed on the page. But in a nice format like something ebay where it shows the picture on the left and the title in the middle. Whereas I just have a list.
I can only seem to find tutorials which just echo out all the information which I don't think I can style around to make it look like what I want. I also tried using a variable to store it in, but I only get the last thing in my database showing up.
Here is some of my code:
$query = mysql_query("SELECT * FROM BoatsForRent") or die("Error Searching");
$count = mysql_num_rows($query);
if ($count == 0) {
$output = 'No results found';
} else {
while ($row = mysql_fetch_array($query))
{
$id = $row['boatId'];
$title = $row['title'];
$type = $row['type'];
$address = $row['address'];
$city = $row['city'];
$postcode = $row['postcode'];
$rooms = $row['rooms'];
$decks = $row['decks'];
$price = $row['price'];
$title = $row['title'];
echo "$title<br>";
}
}
To style the output of the table, you can do some CSS like this:
strong {background: #0ff; text-align: right; display: inline-block; width: 50px;}
span {display: inline-block;}
And you need to give your HTML as:
echo "<strong>Title</strong> <span>$title</span><br>";
Preview
Fiddle: http://jsfiddle.net/praveenscience/WBh52/
Use divs, spans or tables to display your data in any way you wish to. Here is an e.g.
<table boder="0" cellspacing="5" cellpadding="5">
<tr>
<td>Boat ID</td>
<td>Title</td>
<td>Type</td>
<td>Address</td>
<td>City</td>
<td>Post Code</td>
<td>Rooms</td>
<td>Decks</td>
<td>Price</td>
</tr>
<?php
$id = $row['boatId'];
$title = $row['title'];
$type = $row['type'];
$address = $row['address'];
$city = $row['city'];
$postcode = $row['postcode'];
$rooms = $row['rooms'];
$decks = $row['decks'];
$price = $row['price'];
$count =mysql_num_rows($query);
if($count == 0){
$output = 'No results found';
} else {
while ($row = mysql_fetch_array($query))
{
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['title']; ?></td>
<td><?php echo $row['type']; ?></td>
<td><?php echo $row['address']; ?></td>
<td><?php echo $row['city']; ?></td>
<td><?php echo $row['postcode']; ?></td>
<td><?php echo $row['rooms']; ?></td>
<td><?php echo $row['decks']; ?></td>
<td><?php echo $row['price']; ?></td>
</tr>
<?php
}
?>
</table>
Alternatively you can put complete table structure inside echo ''. You can now use css either external or inline as per you wish.
You need to put the data into html tags. Here are just some very basic examples. Then you need to use CSS to add styles.
$query = mysql_query("SELECT * FROM BoatsForRent") or die("Error Searching");
$count = mysql_num_rows($query);
if ($count == 0) {
$output = 'No results found';
} else {
while ($row = mysql_fetch_array($query))
{
$id = $row['boatId'];
$title = $row['title'];
$type = $row['type'];
$address = $row['address'];
$city = $row['city'];
$postcode = $row['postcode'];
$rooms = $row['rooms'];
$decks = $row['decks'];
$price = $row['price'];
$title = $row['title'];
echo "<h1>{$title}</h1>";
echo "<h2>{$type}</h2>";
echo "<h3>{$address}, {$city} {$postcode}</h3>";
echo "<p>This boat has {$rooms} rooms and {$decks} decks!</p>";
echo "<p>The selling price is ${$price}</p>";
}
}
The code you have regarded should be in your head section then the output of while should be changed to arrays:
...
while ($row = mysql_fetch_array($query))
{
$id[] = $row['boatId'];
$title[] = $row['title'];
$type[] = $row['type'];
$address[] = $row['address'];
$city[] = $row['city'];
$postcode[] = $row['postcode'];
$rooms[] = $row['rooms'];
$decks[] = $row['decks'];
$price[] = $row['price'];
$title[] = $row['title'];
}
...
In the body section of your page choose good design for a grid or table and use for loop to populate the data:
<table>
<tr>
<td>ID</td>
<td>Title</td>
....
<?php for ($i =0; $i < count($id); $i++){ ?>
<tr>
<td><?php echo $id[$i];?></td>
<td><?php echo $title[$i];?></td>
....
<?php } ?>
</table>
My code is working right but i have a problem , i can search by the first select option only and the second select option didn't work, the first select option ($_REQUEST["area"]<>'')" and "
($_REQUEST["area"]<>'') is working good but i can fetch only by the
firs select option i write it and else give me error and when change
those i have the same problem, so i want to using two select option to
filter my search.
<?php
if ($_REQUEST["string"]<>'') {
$search_string = " AND (name LIKE '%".mysql_real_escape_string($_REQUEST["string"])."%' OR
specialization LIKE '%".mysql_real_escape_string($_REQUEST["string"])."%' OR
address LIKE '%".mysql_real_escape_string($_REQUEST["string"])."%' OR
telephone LIKE '%".mysql_real_escape_string($_REQUEST["string"])."%' OR
time LIKE '%".mysql_real_escape_string($_REQUEST["string"])."%'
)";
}
if ($_REQUEST["area"]<>'') {
$search_area = " AND location_id='".mysql_real_escape_string($_REQUEST["area"])."'";
}
if ($_REQUEST["category"]<>'') {
$search_category = " AND cate_id='".mysql_real_escape_string($_REQUEST["category"])."'";
}
else {
$sql = "SELECT * FROM informations WHERE id>0".$search_string.$search_area.$search_category;
}
$sql_result = mysql_query ($sql) or die ('request "Could not execute SQL query" '.$sql);
if (mysql_num_rows($sql_result)>0) {
while ($row = mysql_fetch_assoc($sql_result)) {
?>
<tr>
<td><?php $sel_cate2 = "SELECT title FROM categories where id = ".$row['cate_id']." ";
$done_cate2 = mysql_query($sel_cate2);
$get2 = mysql_fetch_array($done_cate2);
echo $get2["title"]; ?></td>
<td><?php $sel_cate2 = "SELECT location FROM locations where id = ".$row['location_id']." ";
$done_cate2 = mysql_query($sel_cate2);
$get2 = mysql_fetch_array($done_cate2);
echo $get2["location"]; ?></td>
<td><?php echo $row["name"]; ?></td>
<td><?php echo $row["specialization"]; ?></td>
<td><?php echo $row["address"]; ?></td>
<td><?php echo $row["telephone"]; ?></td>
<td><?php echo $row["time"]; ?></td>
</tr>
<?php
}
} else {
?>
<tr><td colspan="5">No results found.</td>
<?php
}
?>
</table>