Adding a row to another row - php

I am trying to get the firstname and lastname rows together in one row.
This is the code I have so far ,
$sql = "SELECT firstname, lastname, phone, department FROM tl_member ORDER BY firstname LIMIT 29";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table><tr><br><hr><th align='left'>Naam</th><th align='left'>telefoon<th align='left'>phone</th><th>afdeling</tr>";
// output data in rows
while($row = $result->fetch_assoc()) {
echo "<tr><td>" .$row["firstname"]."</td><td>" .$row["lastname"]."</td><td>" .$row["phone"]."</td><td>" .$row["department"]."</tr>";
}
echo "</table>";
} else {
echo "0 results";
}
the firstname and lastname are now shows seperatly I want them to be together as one ?

Almost there! You just needed to place first name and second name in the same td
echo "<tr><td>" . $row["firstname"] . " " . $row["lastname"] . "</td><td>" . $row["phone"] . "</td><td>" . $row["department"] . "</tr>";

I think you mean this:
$sql = "SELECT firstname, lastname, phone, department FROM tl_member ORDER BY firstname LIMIT 29";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table><tr><br><hr><th align='left'>Naam</th><th align='left'>telefoon<th align='left'>phone</th><th>afdeling</tr>";
// output data in rows
while($row = $result->fetch_assoc()) {
echo "<tr><td>" .$row["firstname"]." " .$row["lastname"]."</td><td>" .$row["phone"]."</td><td>" .$row["department"]."</tr>";
}
echo "</table>";
} else {
echo "0 results";
}

Huum..
Like that ?
$sql = "SELECT firstname, lastname, phone, department FROM tl_member ORDER BY firstname LIMIT 29";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table><tr><br><hr><th align='left'>Naam</th><th align='left'>telefoon<th align='left'>phone</th><th>afdeling</tr>";
// output data in rows
while($row = $result->fetch_assoc()) {
echo "<tr><td>" .$row["firstname"]." ".$row["lastname"]."</td><td>" .$row["phone"]."</td><td>" .$row["department"]."</tr>";
}
echo "</table>";
} else {
echo "0 results";
}

I believe you mean in the same row/cell? Like this?
while($row = $result->fetch_assoc()) {
echo "<tr><td>" .$row["firstname"] . $row["lastname"]."</td><td>" .$row["phone"]."</td>
<td>".$row["department"]."</tr>";
}

write the query in this manner :
$sql = "SELECT firstname, lastname, phone, department,concat(firstname,'',lastname) fullname FROM tl_member ORDER BY firstname LIMIT 29";
$result = $conn->query($sql);
to get fullname you need to use: $row["fullname"]

Related

Count multiple columns from different tables php

I'm trying to count two different tables but it's not working, and I cannot find out where the problem is, and how to implement it with PHP.
$sql = "SELECT COUNT(users.user_id) AS totalUsers FROM users, SELECT COUNT(bikes.bike_id) AS totalBikes FROM bikes";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "<td><td>" . $row["totalUsers"] . "</td></tr>";
echo "<td><td>" . $row["totalBikes"] . "</td></tr>";
}
} else {
echo "0 results";
}
$conn->close();
Get the amounts from each table separately. Each SELECT is a unique resultset from your query, listing them with commas won't retrieve all data as you are expecting.
$result = $conn->query("SELECT COUNT(users.user_id) AS totalUsers FROM users");
$totalUsers = 0;
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
$totalUsers = $row["totalUsers"];
}
$result = $conn->query("SELECT COUNT(bikes.bike_id) AS totalBikes FROM bikes");
$totalBikes = 0;
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
$totalBikes = $row["totalBikes"];
}
$conn->close();
Then you can work with the values stored in $totalUsers and $totalBikes:
echo "<tr><td>" . $totalUsers . "</td></tr>";
echo "<tr><td>" . $totalBikes . "</td></tr>";

Select Data by Id PHP

I have trouble to select a set of specific data using ID from the database. For example, employee one has a unique id of e000000001, when I click the view button in the index will lead to employee detail page which shows the detail of that particular employee instead of all the employees' detail. Thank you.
//from index.php page
<?php
require_once 'db/dbEmpList.php';
$sqlStr = "SELECT * FROM employees;";
$result = $connection->query($sqlStr);
if ($result->num_rows > 0) {
echo "<table class='table table-sm'><thread><tr><th>Full Name</th><th>Employee ID</th><th>Position</th><th>View Employee's Details</th></tr>";
while ($row = $result->fetch_assoc()) {
echo "<tr><td>"
. $row["empName"]. "</td><td>"
. $row["empID"]. "</td><td>"
. $row["position"]. "</td>"
. "<td> <a href='employeedetail.php?id={$row["empID"]}'>View</a>"
. "</td></tr>";
}
}
// from employee page
require_once 'db/dbEmpDetail.php';
$sql = "SELECT * FROM employees where empID = '{$row["empID"]}' ";
$result = mysqli_query($connection, $sql);
if (mysqli_num_rows($result)) {
while ($row = mysqli_fetch_assoc($result)) {
echo '<tr>' .'<td>' .$row["empName"].'</td>'.'<td>'. $row["position"].'</td>' .'<td>'.$row["empNRIC"].'</td>' .'<td>'.$row["empID"].'</td>' .'<td>'.$row["empEmail"].'</td>' .'<td>'.$row["empPwd"].'</td>' . "</tr>";
}
} else {
echo "0 results";
}
mysqli_close($connection);
?>
// FROM EMPLOYEE PAGE
The way you retrieve URL query string is wrong. You should be using $_GET to get the query string from URL. In your case it should be $_GET['id']. See the code below:
require_once 'db/dbEmpDetail.php';
$employeeid = trim(mysqli_real_escape_string($_GET['id']));
$sql = "SELECT * FROM employees where empID = '".$employeeid."' ";
$result = mysqli_query($connection, $sql);
if (mysqli_num_rows($result)) {
while ($row = mysqli_fetch_assoc($result)) {
echo '<tr>' .'<td>' .$row["empName"].'</td>'.'<td>'. $row["position"].'</td>' .'<td>'.$row["empNRIC"].'</td>' .'<td>'.$row["empID"].'</td>' .'<td>'.$row["empEmail"].'</td>' .'<td>'.$row["empPwd"].'</td>' . "</tr>";
}
}
else {
echo "0 results";
}
mysqli_close($connection);
?>

Query gives a blank page

I want to select name, phone from approval(tableB) using condition in users(tableA), I got the below code
$email = 'email';
$sql = "SELECT phone, name FROM approval RIGHT JOIN users ON users.email=email.$assigned";
$result = $conn->query($sql);
if ($result) { // output data of each row
while($row = $result->fetch_assoc()) {
echo "<center><table class='table table-bordered'><thead><tbody><tr><td>Name</td><td>" . $row["name"]. " </td></tr><tr><td>Phone</td><td> " . $row["phone"]. " </td></tr><tr><td></td><td> ".file_get_contents("jquery.php")."</td></tr></thead></tbody‌​> </center>";
}
echo "</table>";
} else {
echo "<center>!Sorry you have not been paired.</center>";
}
}
$conn->close();
?>
on running it, it gives me a blank page.

Prev and Next buttons

I'm new and still learning php&mysql. Search all day and tried different tutorials but nothing happen. So far I have only this with which I fetch text from my DB. How can I make prev and next buttons here?
if($q = mysqli_query($con, 'SELECT * FROM joke WHERE `id` = ' . mysqli_real_escape_string($con,$_GET['id']))){
if($row = mysqli_fetch_array($q)){
echo nl2br($row['text']);
echo '<div id="data">Date ' . $row['date'] . "</div>';
} else {
echo 'Not found';
}
} else {
echo mysqli_error($con);
}
echo "</div>";
$query = "select * from joke order by RAND() LIMIT 1";
$result = mysqli_query($con, $query) or die("Query failed: " . mysqli_errno($con));
while ($row = mysqli_fetch_array($result, MYSQL_BOTH)){
echo 'Random';
}
if i understand you correctly you want to display the next and previous joke based on id,
so you want to select max(id) from joke where id < the current id
and ... select min(id) from joke where id > the current id
$currentId = mysqli_real_escape_string($con,$_GET['id']);
if($q = mysqli_query($con, 'SELECT *,
(SELECT IFNULL(max(id),-1) FROM joke WHERE `id` < '.$currentId.') as previousid,
(SELECT IFNULL(min(id),-1) FROM joke WHERE `id` > '.$currentId.') as nextid
FROM joke WHERE `id` = ' . $currentId)){
if($row = mysqli_fetch_array($q, MYSQL_BOTH)){
echo nl2br($row['text']);
echo '<div id="data">Date ' . $row['date'] . '</div>';
if ($row['previousid'] > -1){
echo 'Previous';
}
if ($row['nextid'] > -1){
echo 'Next';
}
} else {
echo 'Not found';
}
} else {
echo mysqli_error($con);
}
echo "</div>";
$query = "select * from joke order by RAND() LIMIT 1";
$result = mysqli_query($con, $query) or die("Query failed: " . mysqli_errno($con));
while ($row = mysqli_fetch_array($result, MYSQL_BOTH)){
echo 'Random';
}

Running if database row number is less than 10

I'm trying to make a function work only if the number in the row 'click' in my datbase is less than 10. This is what I have right now: I put the echo in there just to see if the condition is working or not.
<?php
include'connect.php';
$result = mysqli_query($con,"SELECT id, link_name, click FROM clicks");
while($row = mysqli_fetch_array($result))
{
if ($row['click'] < 10) {
echo $row['id'] . " " . $row['link_name']. " " .$row['click'];
echo "<br>";
}
}
mysqli_close($con);
?>
try;
$result = mysqli_query($con,"SELECT id, link_name, click FROM clicks WHERE click<10");
while($row = mysqli_fetch_array($result))
{
echo $row['id'] . " " . $row['link_name']. " " .$row['click'];
echo "<br>";
}
mysqli_close($con);
?>
Why don't you use a where clause in your query?
$result = mysqli_query($con,"SELECT id, link_name, click FROM clicks
where click <10");

Categories