$_SESSION problem - I have same ID on all <tr> - php

I don’t know, how I do this: I want to add $id from database to $_SESSION["dbID"] and after click, it shows me more information from database. But table generates in while function and $_SESSION["dbID"] every time set to the highest number of row from table. Please, Can you anyone change my code as I have $_SESSION["dbID"] on every <tr> of table different? Thank you
while($row = $result->fetch_assoc())
{
$id=$row['ID'];
$name=$row['Name'];
$subject=$row['Subject'];
$date=$row['Date'];
echo
'<tr class="trX" id="'.$id.'" href="google.com&id='.$row['ID'].'">
<td class="tdX"><a style="color:black; text-decoration: none;" href="page.php">' . $id . '</a></td>
<td class="tdX"><a style="color:black; text-decoration: none;" href="page.php">' . $name . '</td>
<td class="tdX"><a style="color:black; text-decoration: none;" href="page.php">' . $subject . '</td>
<td class="tdX"><a style="color:black; text-decoration: none;" href="page.php">' . $date . '</td>
</tr>';
}
$_SESSION["dbID"] = $id;
echo ' </table> ';

Explanation
You can do away with all of the a tags and use JavaScript to handle the redirect...
$url = "/path/to/file.php?id=" . $id;
Set the URL to the page that you want to link to. The line above shows the link to the file "file.php" on the server with the query string "id=$id".
onclick="window.location.href='...'"
The line above is a JS equivalent of href. If you want to navigate to a server outside of your domain remember to add the full url e.g. https://www.website.com
Code example
while ($row = $result->fetch_assoc()) {
$id = $row['ID'];
$name = $row['Name'];
$subject = $row['Subject'];
$date = $row['Date'];
$url = "/url/path.php?id=" . $id;
echo <<<EOT
<tr class="trX" onclick="window.location.href='{$url}'">
<td class="tdX">{$id}</td>
<td class="tdX">{$name}</td>
<td class="tdX">{$subject}</td>
<td class="tdX">{$date}</td>
</tr>
EOT;
}
echo ' </table> ';

if (mysqli_num_rows($sql) > 0) {
$row = mysqli_fetch_assoc($sql);
}
while($row = $result->fetch_assoc()){ ?>
<tr class="trX" id="'.<?php echo $row['ID']; ?>.'" href="google.com&id='.$row['ID'].'">
<td class="tdX"><a style="color:black; text-decoration: none;" href="page.php"><?php echo $row['ID']; ?></a></td>
<td class="tdX"><a style="color:black; text-decoration: none;" href="page.php"><?php echo $row['Name']; ?></td>
<td class="tdX"><a style="color:black; text-decoration: none;" href="page.php"><?php echo $row['Subject']; ?></td>
<td class="tdX"><a style="color:black; text-decoration: none;" href="page.php"><?php echo $row['Date']; ?> </td>
</tr>
<?php } ?>
Dont define the rows just make a call for them in the databse.

Related

How to apply nth-child to tables generated from database

I'm having trouble setting up some css code for a table I'm generating using php from a database. I want every even row to have a different background-color, so I tried using nth-child(even) for this, but it doesn't seem to work for some reason. Here's my code:
style.css:
#usertable {
border-collapse: collapse;
font-family: "Trebuchet MS", Arial;
}
#usertable td, #usertable th {
border: 1px solid black;
background-color: rgb(228,227,227);
padding: 8px;
}
#usertable tr:nth-child(odd){background-color: rgb(115,115,115);}
admin.php:
<table id="usertable">
<tr>
<th> Id: </th>
<th> Username: </th>
<th> Rights: </th>
</tr>
<?php
$userquery = "SELECT id, username, strength FROM users";
$result = $conn->query($userquery) or die($conn->error);
while ($row = $result->fetch_assoc()) { ?>
<tr>
<td> <?php echo $row['id']; ?> </td>
<td> <?php echo $row['username']; ?> </td>
<td>
<form method="post" action="">
<input type="number" min="0" max="255" name="newrights" value=" <?php echo $row['strength']; ?> ">
<input type="text" name="idnumber" hidden="true" value=" <?php echo $row['id']; ?> ">
<input type="text" name="usertochange" hidden="true" value=" <?php echo $row['username']; ?> ">
<input type="submit" value="Update">
</form>
</td>
</tr>
<?php
}
?>
</table>
There is some markup error with your code
<table id="usertable">
<tr> <!--Add this-->
<th> Id: </th>
<th> Username: </th>
<th> Rights: </th>
</tr> <!--Add this-->
<?php
$userquery = "SELECT id, username, strength FROM users";
$result = $conn->query($userquery) or die($conn->error);
while ($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row['id'] . "</td><td>" . $row['username'] . "</td><td>";
echo "<form method=\"post\" action=\"\">
<input type=\"number\" min=\"0\" max=\"255\" name=\"newrights\" value=\"" . $row['strength'] . "\">
<input type=\"text\" name=\"idnumber\" hidden=\"true\" value=\"" . $row['id'] . "\">
<input type=\"text\" name=\"usertochange\" hidden=\"true\" value=\"" . $row['username'] . "\">
<input type=\"submit\" value=\"Update\">
</form></td></tr>";
}
?>
Also looks like you have used many slaces so i suggest you to check table markup on browser
Updated: Add
#usertable td:nth-child(odd) { background-color: #efefef;}
Do not use tr as you have given background color to td and th initially
Why don't you sue some CSS class in each alternate table row?
Ex:
HTML
<tr class="bg-red">
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr class="bg-blue">
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
CSS
<style type="text/css">
.bg-blue{
background-color: blue;
}
.bg-red{
background-color: red;
}
</style>
I think this will simplify your code & make it easy to read.
Example for Dynamic Data
<?php
$users[] = array('fname'=>'Jill', 'lname'=>'Smith', 'age'=>50);
$users[] = array('fname'=>'Eve', 'lname'=>'Jackson', 'age'=>94);
$rowColClass = 'bg-blue';
foreach ($users as $key => $value)
{
echo "<tr class='$rowColClass'>";
echo "<td>$value[fname]</td>";
echo "<td>$value[lname]</td>";
echo "<td>$value[age]</td>";
echo "</tr>";
if($rowColClass == 'bg-blue')
$rowColClass = 'bg-red';
else
$rowColClass = 'bg-blue';
}
?>

How to remove repeating headings

spent hours unsuccessfully trying to figure this out, just not a good coder. I have 2 headings called title and date and I'm trying to stop them from repeating before every row. I just want the heading once at the top. What do I have to change to accomplish this. Thanks!!!! Use bitcoin to search.
<thead>
<tr>
<th class='table-header' width='20%'>Title</th>
<th class='table-header' width='5%'>Date</th>
</tr>
</thead>
<?php
define("ROW_PER_PAGE",15);
require_once('db.php');
?>
<html>
<head>
<style>
body{width:615px;font-family:arial;letter-spacing:1px;line-height:20px;}
.tbl-qa{width: 100%;font-size:0.9em;background-color: #f5f5f5;}
.tbl-qa th.table-header {padding: 5px;text-align: left;padding:10px;}
.tbl-qa .table-row td {padding:10px;background-color: #FDFDFD;vertical-align:top;}
.button_link {color:#FFF;text-decoration:none; background-color:#428a8e;padding:10px;}
#keyword{border: #CCC 1px solid; border-radius: 4px; padding: 7px;background:url("demo-search-icon.png") no-repeat center right 7px;}
.btn-page{margin-right:10px;padding:5px 10px; border: #CCC 1px solid; background:#FFF; border-radius:4px;cursor:pointer;}
.btn-page:hover{background:#F0F0F0;}
.btn-page.current{background:#F0F0F0;}
</style>
</head>
<body>
<?php
$search_keyword = '';
if(!empty($_POST['search']['keyword'])) {
$search_keyword = $_POST['search']['keyword'];
$sql = 'SELECT * FROM posts WHERE post_title LIKE :keyword OR description LIKE :keyword OR post_at LIKE :keyword ORDER BY id DESC ';
/* Pagination Code starts */
$per_page_html = '';
$page = 1;
$start=0;
if(!empty($_POST["page"])) {
$page = $_POST["page"];
$start=($page-1) * ROW_PER_PAGE;
}
$limit=" limit " . $start . "," . ROW_PER_PAGE;
$pagination_statement = $pdo_conn->prepare($sql);
$pagination_statement->bindValue(':keyword', '%' . $search_keyword . '%', PDO::PARAM_STR);
$pagination_statement->execute();
$row_count = $pagination_statement->rowCount();
if(!empty($row_count)){
$per_page_html .= "<div style='text-align:center;margin:20px 0px;'>";
$page_count=ceil($row_count/ROW_PER_PAGE);
if($page_count>1) {
for($i=1;$i<=$page_count;$i++){
if($i==$page){
$per_page_html .= '<input type="submit" name="page" value="' . $i . '" class="btn-page current" />';
} else {
$per_page_html .= '<input type="submit" name="page" value="' . $i . '" class="btn-page" />';
}
}
}
$per_page_html .= "</div>";
}
$query = $sql.$limit;
$pdo_statement = $pdo_conn->prepare($query);
$pdo_statement->bindValue(':keyword', '%' . $search_keyword . '%', PDO::PARAM_STR);
$pdo_statement->execute();
$result = $pdo_statement->fetchAll();
}
?>
<form name='frmSearch' action='' method='post'>
<div style='text-align:right;margin:20px 0px;'><input type='text' name='search[keyword]' value="<?php echo $search_keyword; ?>" id='keyword' maxlength='25'></div>
<?php
if(!empty($result)) {
foreach($result as $row) {
?>
<table class='tbl-qa'>
<thead>
<tr>
<th class='table-header' width='20%'>Title</th>
<th class='table-header' width='5%'>Date</th>
</tr>
</thead>
<tbody id='table-body'>
<tr class='table-row'>
<td><a style="text-decoration: none;" href="<?php echo $row['description']; ?>">
<?php echo $row['post_title']; ?></a></td>
<td><?php echo $row['post_at']; ?></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
<?php echo $per_page_html; ?>
</form>
</body>
</html>
This way it will works:
Array to test (use your $result in your case)
$result=array(
array('post_title'=>'title 1','post_at'=>'at 1'),
array('post_title'=>'title 2','post_at'=>'at 2'),
array('post_title'=>'title 3','post_at'=>'at 3'),
)
Html:
<table class='tbl-qa'>
<tr>
<th class='table-header' width='20%'>Title</th>
<th class='table-header' width='5%'>Date</th>
</tr>
<?php
if(!empty($result)) {
foreach($result as $row) {
?>
<tr class='table-row'>
<td><a style="text-decoration: none;" href="<?php echo $row['description']; ?>">
<?php echo $row['post_title']; ?></a></td>
<td><?php echo $row['post_at']; ?></td>
</tr>
<?php
}
?>
<?php
}
?>
As you can see, the header if written just once because it is out of the loop whilst the row inside the loop will be repeated.
Finally close the table out of the loop.

save values from editable table using php

Hi I have a table generated from php, it is editable, I want to save edited values to database. I have no Idea how can I do this as there are many rows(dynamic) on a page.
Here is a screen-shot:-
Please suggest
Edit:-
My code is
echo "<table border='1'>
<tr>
<th>Sl Number</th>
<th>Product Id</th>
<th>Product Name</th>
<th>Product Catagory</th>
<th>Retal Price</th>
<th>Max Price</th>
<th>Min Price</th>
<th>Initial Stock</th>
<th>Quantity Sold</th>
<th>Current Stock</th>
<th>Last Order</th>
<th>Edit/Update</th>
</tr>";
while($row = $result->fetch_assoc())
{
echo "<tr contenteditable>";
echo "<td>" . $row["Row Number"]. "</td>";
echo "<td>" . $row["Product Id"]. "</td>";
echo "<td>" . $row["Product Name"]. "</td>";
echo "<td>" . $row["Product Catagory"]. "</td>";
echo "<td>" . $row["Retal Price"]. "</td>";
echo "<td>" . $row["Max Price"]. "</td>";
echo "<td>" . $row["Min Price"]."</td>";
echo "<td>" . $row["Initial Stock"]."</td>";
echo "<td>" . $row["Quantity Sold"]. "</td>";
echo "<td>" . $row["Current Stock"]."</td>";
echo "<td>" . $row["Last Order"]."</td>";
echo "<td contenteditable = 'false'><button href = '#'>Update</a></td>";
echo "</tr>";
}
Let me give you with the best way
First your database tables have spaces: correct that e.g.
from $row["Initial Stock"] to $row["Initial_Stock"]
Then i will propose you use ajax instead of wasting time clicking buttons
The HTML Page
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-beta1/jquery.min.js"></script>
<script>
$(function(){
$("#loading").hide();
//acknowledgement message
var message_status = $("#status");
$("td[contenteditable=true]").blur(function(){
var field_userid = $(this).attr("id") ;
var value = $(this).text() ;
$.post('update.php' , field_userid + "=" + value, function(data){
if(data != '')
{
message_status.show();
message_status.text(data);
//hide the message
setTimeout(function(){message_status.hide()},1000);
}
});
});
});
</script>
<style>
table.zebra-style {
font-family:"Lucida Sans Unicode", "Lucida Grande", Sans-Serif;
text-align:left;
border:1px solid #ccc;
margin-bottom:25px;
width:100%
}
table.zebra-style th {
color: #444;
font-size: 13px;
font-weight: normal;
padding: 5px 4px;
}
table.zebra-style td {
color: #777;
padding: 4px;
font-size:13px;
}
table.zebra-style tr.odd {
background:#f2f2f2;
}
#status { padding:10px; position:fixed; top:10px; border-radius:5px; z-index:10000000; background:#88C4FF; color:#000; font-weight:bold; font-size:12px; margin-bottom:10px; display:none; width:100%; }
#loading { padding:10px; position:absolute; top:10px; border-radius:5px; z-index:10000000; background:#F99; color:#000; font-weight:bold; font-size:12px; margin-bottom:10px; display:none; width:100%; }
</style>
<div id="status"> </div>
<div id="loading"></div>
<table id="tableID" border="0" class="sortable table zebra-style">
<thead>
<tr>
<th>Sl Number</th>
<th>Product Id</th>
<th>Product Name</th>
<th>Product Catagory</th>
<th>Retal Price</th>
<th>Max Price</th>
<th>Min Price</th>
<th>Initial Stock</th>
<th>Quantity Sold</th>
<th>Current Stock</th>
<th>Last Order</th>
<th>Edit/Update</th>
</tr>
</thead>
<tbody class="list">
<?php do { ?>
<tr>
<td contenteditable="true" id="Row_Number:<?php echo $row["Row_Number"]; ?>"><?php echo $row["Row_Number"]; ?></td>
<td contenteditable="true" id="Product_Id:<?php echo $row["Product_Id"]; ?>"><?php echo $row["Product_Id"]; ?></td>
<td contenteditable="true" id="Product_Name:<?php echo $row["Product_Name"]; ?>"><?php echo $row["Product_Name"]; ?></td>
<td contenteditable="true" id="Product_Catagory:<?php echo $row["Product Id"]; ?>"><?php echo $row["Product_Catagory"]; ?></td>
<td contenteditable="true" id="Retal_Price:<?php echo $row["Retal_Price"]; ?>"><?php echo $row["Retal_Price"]; ?></td>
<td contenteditable="true" id="Max_Price:<?php echo $row["Max_Price"]; ?>"><?php echo $row["Max_Price"]; ?></td>
<td contenteditable="true" id="Min_Price:<?php echo $row["Min_Price"]; ?>"><?php echo $row["Min_Price"]; ?></td>
<td contenteditable="true" id="Initial_Stock:<?php echo $row["Initial_Stock"]; ?>"><?php echo $row["Initial_Stock"]; ?></td>
<td contenteditable="true" id="Quantity_Sold:<?php echo $row["Quantity_Sold"]; ?>"><?php echo $row["Quantity_Sold"]; ?></td>
<td contenteditable="true" id="Last_Order:<?php echo $row["Last_Order"]; ?>"><?php echo $row["Last_Order"]; ?></td>
<td contenteditable="true" id="Last_Order:<?php echo $row["Last_Order"]; ?>"><?php echo $row["Last_Order"]; ?></td>
<td contenteditable = 'false'></td>";
</tr>
<?php } while($row = $result->fetch_assoc()) ?>
</tbody>
</table>
And the update php page
<?php
$db = new PDO('mysql:host=localhost;dbname=testdb;charset=UTF-8',
'username',
'password',
array(PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
?>
<?php
if(!empty($_POST))
{
//database settings
foreach($_POST as $field_name => $val)
{
//clean post values
$field_id = strip_tags(trim($field_name));
//from the fieldname:user_id we need to get user_id
$split_data = explode(':', $field_id);
$product_id = $split_data[1];
$field_name = $split_data[0];
if(!empty($product_id) && !empty($field_name) && !empty($val))
{
$affected_rows = $db->exec("UPDATE yourtable SET $field_name = '$val' WHERE product_ID = $product_id");
echo $affected_rows;
echo "Updated";
} else {
echo "Invalid Requests";
}
}
}
else {
echo "Invalid Requests";
}
?>
To save the whole table, you could leave the row-level update buttons out, and have a single save button:
<button id="save">Save</button>
<div id="msg"></div>
The msg area is to display feed-back from the server when the save-operation is performed.
Then you would add this JavaScript to handle the click of the save button:
$('#save').click(function() {
var data = [];
$('td').each(function() {
var row = this.parentElement.rowIndex - 1; // excluding heading
while (row >= data.length) {
data.push([]);
}
data[row].push($(this).text());
});
$.post('savetable.php', {table: data}, function (msg) {
$('#msg').text(msg);
});
});
This will transform the HTML table contents, without header row, to a JavaScript matrix, which then is sent to savetable.php for processing.
In PHP you would then use $_POST['table'] to access that array. When you implement this, first debug, and do a var_dump($_POST['table']) to make sure the data was transferred correctly, and it has the array structure you expect.
Then loop over that array to insert the rows into your database. You can use mysqli or PDO for this.
The PHP script savetable.php should only echo one message: a confirmation ("the table was saved successfully") or an error message ("a problem occurred. Your data was not saved.").
It should not reproduce the HTML of the table, since that is already displayed in the browser. Whatever PHP prints will be used by the JavaScript code to be displayed below the save button.
Here is how savetable.php could look like. But please debug carefully, I did not test this code. And before it works, you first need to set up your database model of course:
$db = new PDO('mysql:host=localhost;dbname=testdb;charset=utf8mb4',
'username', 'password');
// Configure that all database errors result in an exception:
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try {
// Make a transaction: this allows to rollback any of the changes
// if anything goes wrong before the end of it.
$db->beginTransaction();
// First delete all rows.
$db->exec("DELETE FROM mytable");
// Prepare a statement that will be executed for each row.
// Needs to be extended for all columns:
$stmt = $db->prepare("INSERT INTO mytable (sl_num, product_id, product_name)
VALUES (?, ?, ?)");
foreach ($_POST('table'] as $row) {
$stmt->execute($row); // the columns are passed to the prepared statement.
}
// All went well: commit the changes.
$db->commit();
echo "Table saved successfully";
} catch(PDOException $e) {
// Something went wrong: roll back any changes made
$db->rollback();
echo "An error occurred: " . $e->getMessage();
}
What you can do is ajax call to a php with the id of the row you want to save and the new data. Then use PDO to connect to the database and update the information. After that is done, use javascript to edit the table itself.
So what you need to do is look up how to use ajax calls and use PDO. I suggest when you echo the table itself
<button href = '#' onclick="updateRow('. $row['Row Number'] .')">Update</a></td> where Row Number is the ID in the database and updateRow() is the javascript function you will create to get the new information and send it via ajax. Don't use mysql_*, because it is not supported in php 7 and it will die soon. Look up PDO instead.

Unable to delete first row of database

I have a page to view and delete data from database. The delete is a link that clicked and pass the variable in url to another page for delete query. For example i have 3 rows in total in my database, i have no problem in deleting the last two rows but i was unable to delete the first row because the delete is deleting the record based on the "description" column, not the "ID" column. Below is the submission page and delete query page
viewreportdb.php
<!doctype html>
<head>
<meta charset="utf-8">
<title>Report Database </title>
<link href="style/fg_membersite.css" rel='stylesheet' type='text/css'>
</head>
<?php
include('config.php');
include('adminconfig.php');
$sql = "SELECT * FROM report ORDER BY ID";
$result = mysql_query($sql);
?>
<body>
<form method="POST" action="reportsave.php" onSubmit="return validate(this);">
<table id='fcolor2' class="viewdb3">
<tr>
<td style='border:2px solid black; background-color:#FF9933;' align=center> ID </td>
<td style='border:2px solid black; background-color:#FF9933;' align=center> Name </td>
<td style='border:2px solid black; background-color:#FF9933; width:auto;' align=center> Department </td>
<td style='border:2px solid black; background-color:#FF9933; width:auto;' align=center> Description </td>
<td style='border:2px solid black; background-color:#FF9933; width:auto;' align=center> Date </td>
</tr><?!-- missing close tr -->
<?php
while($row = mysql_fetch_array($result))
{
?>
<tr>
<td style='border:2px solid black; background-color:#33FF00; width:auto' align=center><?php echo $row['ID'] ?></td>
<td style='border:2px solid black; background-color:#33FF00; width:auto' align=center><?php echo $row['name'] ?></td>
<td style='border:2px solid black; background-color:#33FF00; width:auto' align=center><?php echo $row['department'] ?></td>
<td><textarea rows="5" cols="50" name="description[]"><?php echo $row['description']; ?></textarea></td>
<td style='border:2px solid black; background-color:#33FF00; width:auto' align=center><?php echo $row['date'] ?></td>
</tr><!-- missing close tr -->
<tr>
<td><input class='edit' type = 'text' name="ID[]" value='<?php echo $row['ID'] ?>' maxlength="50"/></td>
<td><input class='edit' type = 'text' name="name[]" value='<?php echo $row['name'] ?>' maxlength="50"></td>
<td><input class='edit' type = 'text' name="department[]" value='<?php echo $row['department'] ?>' maxlength="50"></td>
<td> </td>
<td><input class='edit' type = 'text' name="date[]" value='<?php echo $row['date'] ?>' maxlength="50"></td>
<?php echo "<td><a href='http://localhost/kinhock/deletereport.php?description=".$row['description']."'>Delete</a></td>";?>
</tr><!-- missing close tr -->
<?php
} ?>
<tr class='btn2'><td><input type='submit' name='submit' value='Submit' /></td></tr>
<tr class='btn2'><td> Back to Description </td></tr><!-- missing open & close tr-->
<tr class='btn2'><td> Logout</td></tr>
</table>
</form>
<?php
$ID=$row['ID'];
$name=$row['name'];
$department=$row['department'];
$date=$row['date'];
$description=$row['description'];
?>
</body>
</html>
deletereport.php
<?php
include('adminconfig.php');
include('config.php');
$description = $_GET['description'];
$sql="DELETE FROM `report` WHERE `description`='$description'";
$result=mysql_query($sql);
if(!$result){
die('invalid query:'.mysql_error());
}else
?>
<p style="font-family:arial;color:#0066CC;font-size:30px;">One row deleted...</p>
<?php
header('Refresh:3; url=viewreportdb.php');
die;
?>
Do not delete using description field because urlencoding and many more factors can break this code, even duplicate descriptions are quite possible, isn't it? Also your code is
liable to easiest SQL Injection methods!
Change your code here:
<?php echo "<td><a href='http://localhost/kinhock/
deletereport.php?id=".$row['ID']."'>Delete</a></td>";?>
And your SQL query:
$id = intval($_GET['id']);
$sql="DELETE FROM `report` WHERE `id`='$id'";
STEP 1
Send the ID,not the description, for deletion.
<?php echo "<td><a href='http://localhost/kinhock
/deletereport.php?id=".$row['ID']."'>Delete</a></td>";?>
STEP 2
Delete using the ID, not the description.
$id = intval($_GET['id']);
$sql="DELETE FROM `report` WHERE `id`=$id";
You should probably use the ID instead of the description within your where clause and in this line:
(change)
<?php echo "<td><a href='http://localhost/kinhock
/deletereport.php?description=".$row['description']."'>Delete</a></td>";?>
(to)
<?php echo "<td><a href='http://localhost/kinhock
/deletereport.php?id=".$row['ID']."'>Delete</a></td>";?>
and finally change:
$description = $_GET['description'];
$sql="DELETE FROM `report` WHERE `description`='$description'";
to:
$id = $_GET['id'];
$sql="DELETE FROM `report` WHERE `ID`='$id'";
Using the ID will be faster and more accurate assuming it is a primary or unique ID.
You may also want to use:
$id = mysql_real_escape_string($_GET['id']);
to prevent SQL injection.
I believe your report table's ids are based on the users IDs right? Why don't you make it such that each report had an ID(report ID), User_ID(eg. admin001) and a description. In this way if admin001 makes report A it will have ID 1, if admin001 makes report B it will have ID 2. si when you want to delete Report A you use
$sql = "delete from {tablename} where ID={report ID}";

PHP: working with tr and td with while()

<table border="0" cellspacing="0" cellpadding="0" >
<tr>
<?php
while ($pF = mysql_fetch_array($stringVisits)) {
?>
<td class="viewercell" style="color: #CCC; font-size: 10px;">
<?php
echo "<a href='profil.php?id=".$BuID."'>";
echo "<img style='margin-right: 5px; width: 61px; height: 80px;'";
if (checkStatus($BuID) == 1) {
echo 'class="onlineBorder" ';
} else {
echo 'class="image-xxsmall-border" ';
}
echo " src='images/profilePhoto/thumbs/";
if (!empty($getByProfile["photo_thumb"])) {
echo $getByProfile["photo_thumb"];
} else {
echo "noPhoto_thumb.jpg";
}
echo "'>";
?>
</a>
</td>
<?php } ?>
</tr>
</table>
This is what i have right now it displays the visiter´s profileimage. Now i would like to have their name under the image too.. but then i need to create another <tr> after this one, and add their name in each <td>. But how can i do that, if i have this while? Should i run another while, to get the names or can i do something smart with this?
Why not just stick the name in the same cell as the image? After you close the image tag, just echo $getByProfile["username"], or whatever?
<table border="0" cellspacing="0" cellpadding="0" >
<tr>
<?php
while($pF = mysql_fetch_array($stringVisits)){
?>
<td class="viewercell" style="color: #CCC; font-size: 10px;">
<?php
echo "<a href='profil.php?id=".$BuID."'>";
echo "<img style='margin-right: 5px; width: 61px; height: 80px;'";
if(checkStatus($BuID) == 1){
echo 'class="onlineBorder" ';
} else {
echo 'class="image-xxsmall-border" ';
}
echo " src='images/profilePhoto/thumbs/";
if(!empty($getByProfile["photo_thumb"])) { echo $getByProfile["photo_thumb"]; }else{
echo "noPhoto_thumb.jpg";
}
echo "'><br/>";
?>
</a>
<br/><?php echo $getByProfile['username']; ?>
</td>
<?php } ?>
</tr>
</table>
Here's a way to do it with minimal changes to your existing code:
<?php
<table border="0" cellspacing="0" cellpadding="0" >
<tr>
<?php
$names = array(); // ADDITION #1
while($pF = mysql_fetch_array($stringVisits)){
$names[] = // ADDITION #2 - ADD NEW USER NAME HERE;
?>
<td class="viewercell" style="color: #CCC; font-size: 10px;">
<?php
echo "<a href='profil.php?id=".$BuID."'>";
echo "<img style='margin-right: 5px; width: 61px; height: 80px;'";
if(checkStatus($BuID) == 1){
echo 'class="onlineBorder" ';
} else {
echo 'class="image-xxsmall-border" ';
}
echo " src='images/profilePhoto/thumbs/";
if(!empty($getByProfile["photo_thumb"])) { echo $getByProfile["photo_thumb"]; }else{
echo "noPhoto_thumb.jpg";
}
echo "'>";
?>
</a>
</td>
<?php }
// ADDITION #3:
echo "</tr>
<tr>";
foreach ($names as $username)
echo "<td class=\"username\"><p>$username</p></td>";
?>
</tr>
</table>
Also note that the way you had your code, the </tr> was inside the while loop.
this might not look nice but it should work why do you want 2 loops?
<?php
echo "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" >";
while($pF = mysql_fetch_array($stringVisits)){
echo "
<tr>
<td class="viewercell" style="color: #CCC; font-size: 10px;">
<a href='profil.php?id=".$BuID."'> <img /> </a>
</td>
</tr>
<tr><td> " . $getByProfile['username'] . " </td></tr>";
}
echo "</table>";
?>

Categories