PHP table values - php

I have a question and I will try to explain the best way that I can
I have a table that show me names of accounts and each one of that accounts has it own information.
and I'm getting that information like this
On the page "verpersonagem.php" I have this: $idaccount = $_GET['accountId'];
but the url comes like this ../verpersonagem.php?account=
Why it's not getting the value?
Thanks in advance

you don't have yo use all this echo statement
try this
<?php
while ($row = mysqli_fetch_row($result))
{
echo '<tr>
<th> '. $row[0] .' </th>
<td>'. $row[2] .' </td>
<td>'. $row[1] .'</td>
<td>'. $row[3] .'</td>
<td>'. $row[4] .'</td>
</tr>';
}
?>

You should insert the href into the Table its TD instead of TH
Use a for loop instead and say count(rows)
Then $i < $rows and href=example.php?id=$i
Then use $_GET['id']
And do a database query which gets all the info about the id

Related

How to fetch a specific data from a table and display that specific data in another page?

Hi there:) I have created a table where there's a list of products.
if ($result) {
echo '<table align="center">
<tr><th><b>ID</b></th><th><b>Name</b></th><th><b>Made_In</b>
</th><th><b>Price</b></th></tr>';
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo '<tr><td >' . $row['ID'] . '</td>
<td >' . $row['Name'] . '</td>
<td >' . $row['Made_In'] . '</td>
<td >' . $row['Price'] . '</td></tr>';
}
echo '</table>';
Expected Output:
When the user want to see more details about this particular product, for example Product A. The user just have to click the product name and it will take the user to a new page where the user can see more details about the Product A such as description, ingredient, company's details etc.
Just so you know all of this is using the same table from the database called Product.
I have tried make the product's name as a link but i don't know how to fetch a specific data from the table to a new page.
I know this seems easy but i can't find the answer anywhere. Thank you for your time
<?php
if($result){?>
<table align="center">
<tr><th><b>ID</b></th><th><b>Name</b></th><th><b>Made_In</b>
</th><th><b>Price</b></th></tr>
<?php
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {?>
<tr>
<td ><?php echo $row['ID'];?> </td>
<td ><?php echo $row['Name']; ?></td>
<td ><?php echo $row['Made_In'];?></td>
<td ><?php echo $row['Price'];?></td>
</tr>
}
<?php } ?>
you need to fetch the particular record in new page.
eg: $id = $_GET['id']; // this will receive from url eg: example.com/products?id=2
and fetch the records using that $id.
Now you can use show table with fetched detail.
Suggestion: you can use php inside html. So, its better to write html table tags and open/close php tags where necessary. This will make your code easier to understand.

The Delete link on my records table is not working php

I have a Table that displays titled and first name columns and delete link on the 3rd column.
Unfortunately for a reason i don't understand, records are not deleting when the Delete link is clicked.
Please friends, help me figure out what's wrong here.
<?php
$user_id = $_SESSION["user_id"]; //brought here via
session
//select statement here
// output data of each row
while($row = $result->fetch_assoc()) {
echo '<tr>
<td scope="row">' . $row["titled"]. '</td>
<td> '.$row["firstname"] .'</td>
<td><a href="user_delete.php?
delete=$row[user_id]">Delete</a>
</td>
</tr>';
}
} else {
echo "0 results";
}
?>
user_delete.php code
<?php
session_start();
require_once $_SERVER['DOCUMENT_ROOT'] .
`enter code here`'/soap/includes/server.php';
if(isset($_GET["delete"]) )
{
$user_id = $_GET["delete"];
$sql= "DELETE FROM users WHERE user_id='$user_id'";
$res= mysqli_query($con, $sql) or
die("Failed".mysqli_error($con));
echo "<meta http-equiv='refresh'
content='0;url=user_settings.php'>";
}
?>
While you must debug code and add results for check what could be the issue. But one issue I think is using variable $row[user_id] in single quoted string. PHP do not parse variable in single quoted string. So either transfer all html code from single to double quotes or atleast append $row[user_id] seperatly like other $row[firstname]
echo '<tr>
<td scope="row">' . $row["titled"]. '</td>
<td> '.$row["firstname"] .'</td>
<td><a href="user_delete.php?
delete='.$row["user_id"].'">Delete</a>
</td>
</tr>'

How to number items of foreach loop in php? [duplicate]

This question already has answers here:
How to find the foreach index?
(14 answers)
Closed 7 years ago.
I am using this to create a mp3 list.
$mp3 = glob($directory . '*.mp3');
//print each file name
foreach($mp3 as $mp3)
{
echo '
<tr>
<td class="one"><!--number goes here --></td>
<td class="one">'. str_replace('(BDalbum.com).mp3','',basename($mp3)) .'</td>
<td class="two">'. $_GET['s'] .'</td>
<td class="three">'. $_GET['a'] .'</td>
<td class="two">'. $_GET['a'] .'</td>
</tr>
';
}
I want to add number with every item of the list like, first one: 1, second one:2 etc. how can I do it?
$listOfMp3 = glob($directory . '*.mp3');
foreach($listOfMp3 as $i => $mp3)
{
echo $i;
}
This is not the best solution, but you can use a counter. Here is an example:
$mp3 = glob($directory . '*.mp3');
$c = 0; //Set this value to 1 if you don't want to start with 0
foreach($mp3 as $mp3)
{
echo '
<tr>
<td class="one">'.$c.'</td>
<td class="one">'. str_replace('(BDalbum.com).mp3','',basename($mp3)) .'</td>
<td class="two">'. $_GET['s'] .'</td>
<td class="three">'. $_GET['a'] .'</td>
<td class="two">'. $_GET['a'] .'</td>
</tr>';
$c++;
}
So now $c will display the number of the file every time. Anyway, you can get the foreach key to do exactly the same.

How to display multiple rows one after the other repeating a sequence depending on how many rows I get?

My idea is to create a Q&A section under a product profile, just like on eBay or Amazon or whatever. The idea is to send a question and then get the owner of the article to reply.
The table has these columns: pid (product ID), id (question ID), question, answer, date (date posted), username.
So if I post a question, I get the ID of the product in which I'm posting and create a question. The the owner just sends the answer to the row that matches the question.
Here's my PHP code to retrieve all the info from that table:
$qanda = '';
$link = mysql_connect("localhost", "youknowwhat", "youknowwhat");
mysql_select_db("youknowwhat", $link);
$qandaq = mysql_query("SELECT * FROM questions WHERE id='$id2' ORDER BY date", $link);
$count = mysql_num_rows($qandaq);
if($count >= 1){
while($rows = mysql_fetch_array($qandaq)){
$date = $rows['date'];
$q = $rows['question'];
$a = $rows['answer'];
$usrname = $rows['username'];
}
$qanda .= '<div id="answers" align="center">
<table cellspacing="0" align="center">
<tr align="center">
<td width="200">' . $date . '</td>
<td rowspan="2" width="400"><strong>' . $q . '</strong><br>' . $a . '</td>
<td width="200">Delete</td>
</tr>
<tr align="center">
<td>' . $usrname . '</td>
<td>Report</td>
</tr>
</table>
</div>';
} else {
$qanda = '<div id="answers" align="center">
No questions for this product.
</div>';
}
Now... what you see as a table in the variable $qanda I want to repeat it over and over again but displaying different row data but the concatenation isn't working and I can only get the last row to be displayed. I just can't seem to find out why this isn't working! Am I missing something?
All you have to do is append your divs (.=) while you're inside the while loop that mysql_fetch_array() rows.
Then you'll have a new div for each row your database returns, and you can populate it easily.
$qanda = '';
while($rows = mysql_fetch_array($qandaq)){
$date = $rows['date'];
$q = $rows['question'];
$a = $rows['answer'];
$usrname = $rows['username'];
$qanda .= '<div id="answers" align="center">
<table cellspacing="0" align="center">
<tr align="center">
<td width="200">' . $date . '</td>
<td rowspan="2" width="400"><strong>' . $q . '</strong><br>' . $a . '</td>
<td width="200">Delete</td>
</tr>
<tr align="center">
<td>' . $usrname . '</td>
<td>Report</td>
</tr>
</table>
</div>';
}

Hyperlinks within a table - ''.$row['username'].''

I have made a leaderboard table on my site, which returns the users in the MySQL database with the highest scores (score is a separate field). The fields in the leaderboard table are 'rank' 'username' and 'score'. I would like to link each username in the table to it's own profile page.
The profile pages are in the format /profile.php?user=$username. How would I go about adding an <a href> within the table (which is echoed in PHP):
echo '<tr>
<td>' .$a. '</td>
<td>' .$row['username']. '</td>
<td>'.$row['count'].'</td>
</tr>';
I've tried the above, but it doesn't seem to work. It shows the usernames, but they don't have any hyperlinks.
<?php
$row = array(
'username' => 'Username',
'count' => 5
);
echo '<table><tr><td>'
. $a . '</td><td><a href="profile.php?user='
. $row['username'] . '">'
. $row['username'] . '</a></td><td>'
. $row['count'] . '</td></tr></table>';
?>
This works fine, I don't know what the problem is? I just do not know what $a does though
Looks correct to me. I would paste the (html) source-part it outputs, to get a hint what's really going on.
The retrieval part must be good, if you're seeing your usernames, so something strange is going on.
Writing
echo '<tr> <td>'. $ a. '</ Td> <td> <a href="profile.php?user=' .$row['username'].'">'. $ Row ['username']. . '</ A> </ td> <td>' $ row ['count'] '</ td> </ tr>'.;
is incorrect because of:
</ A>
Correct version is:
echo '<tr> <td>'. $ a. '</ Td> <td> <a href="profile.php?user=' .$row['username'].'">'. $ Row ['username']. . '</ a> </ td> <td>' $ row ['count'] '</ td> </ tr>'.;

Categories