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

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>'.;

Related

How to change column name into a variable

I've written a function which is gonna be used by my team and so far there are no specific actual column names in it except the columns in a **foreach loop**. I'd like to change them in to a variable so my team just change the variables in the given array instead of searching the entire code. Here's a snippet so you can see what I mean.
These are the attributes where you put in the actual name of the column.
$attributes = array("id", "firma", "vorname", "nachname", "straße", "hausnummer", "telefonnr", "dateien", "column", "column");
That's a row in my table which is displayed with echo "code" statement in php. Everytime I change the column name which is used by the foreach loop I'll get an error.
<tr> <!-- display data -->
<td style='text-align:center;' width=" . $width[0] . ">$print->id</td>
<td width=" . $width[1] . ">$print->firma</td>
<td width=" . $width[2] . ">$print->vorname</td>
<td width=" . $width[3] . ">$print->nachname</td>
<td width=" . $width[4] . ">$print->straße</td>
<td style='text-align:center;' width=" . $width[5] . ">$print->hausnummer</td>
<td width=" . $width[6] . ">$print->telefonnr</td>
What I tried so far was
<td width=" . $width[1] . ">$print->" . $attributes[1] . "</td>
or
<td width=" . $width[1] . ">$print->$attributes[1]</td>
Is there a way to change the column name into a variable without getting errors?

I tried to display data that are in the database in to a table but it don't return anything

i'm trying to display data that are already in the database i passed these data in to a table like this
<?php
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_assoc($result)){
var_dump($row);
//echo $last_id;
'<tr>
<td> ' . $row['ord_id'].'</td>
<td> '.$row['ord_total'].'</td>
<td> '.$row['ord_date'].'</td>
<td> '.$row['ord_qty'].'</td>
<td> '.$row['card_id'].'</td>
<td> '.$row['card_price'].'</td>
<td> '.$row['card_field1'].'</td>
<td> '.$row['card_field2'].'</td>
<td> '.$row['card_field3'].'</td>
<td> '.$row['card_field4'].'</td>
<td> '.$row['card_field5'].'</td>
</tr>';
var_dump($row);
}
?>
i used var_dump($row) in several places it returns data but that data is not displaying in the table
I think you are missing echo before creating the table at least in the code you posted. So it should be like this
$row = [
'ord_id' => 1,
'ord_total' => 1,
'ord_date' => 1,
'ord_qty' => 5,
'card_id' => 5,
'card_price' => 123.0,
];
echo '<table><tr>
<td> ' . $row['ord_id'] . '</td>
<td> ' . $row['ord_total'] . '</td>
<td> ' . $row['ord_date'] . '</td>
<td> ' . $row['ord_qty'] . '</td>
<td> ' . $row['card_id'] . '</td>
<td> ' . $row['card_price'] . '</td>
</tr></table>'
;
var_dump() automatically echoes to the output, but strings you create don't. You're creating a string, but never emitting it to the output. The command you're looking for is echo.
For example, this just creates a string but does nothing with it:
'<tr><td>' . $row['ord_id'] . '</td></tr>';
This "echoes" that string to the output:
echo '<tr><td>' . $row['ord_id'] . '</td></tr>';

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.

PHP table values

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

Display several results with one SQL query (group by?)

I want to show several results of one query, but I need to group some of these results. I think it's better for you to see my problem :
I have this table that shows several trainings :
And when you click on a row, it'll expand and show exercises that correspond to the training's id (here's example values) :
To get all the exercises by training, I made the following query :
SELECT E.id_entrainement, E.intitule_entrainement, E.date_entrainement, EX.id_exercice, EX.reps, EX.poids, M.nom_exm FROM entrainements E, exercices EX, exercices_muscles M WHERE EX.id_entrainement = E.id_entrainement AND EX.membre = E.id_membre AND M.id_exm = EX.id_exercice_muscle AND id_membre='".$user_id."' GROUP BY E.id_entrainement
But when I put in my code the $data['id_entrainement'] for example, it shows me only the first value got by the query as you can see here :
And when I remove the GROUP BY clause from my query, I have this :
I don't know if it's really helpful for you, but here's my php code :
echo '<div class="container">';
echo '<h1>Vos entraînements</h1>
<table class="table table-striped">
<thead>
<tr>
<th>#</th>
<th>Référence entraînement</th>
<th>Nom de l\'entraînement</th>
<th>Date de réalisation</th>
<th></th>
</tr>
</thead>
<tbody>';
$i=0;
//$getTrainings = $bdd->query("SELECT * FROM entrainements WHERE id_membre='".$user_id."'");
$getTrainings = $bdd->query("SELECT E.id_entrainement, E.intitule_entrainement, E.date_entrainement, EX.id_exercice, EX.reps, EX.poids, M.nom_exm FROM entrainements E, exercices EX, exercices_muscles M WHERE EX.id_entrainement = E.id_entrainement AND EX.membre = E.id_membre AND M.id_exm = EX.id_exercice_muscle AND id_membre='".$user_id."' ");
while ($data = $getTrainings->fetch()) {
$i++;
echo '
<tr class="plusExpand">
<th scope="row">' . $i . '</th>
<td>' . $data['id_entrainement'] . '</td>
<td>' . $data['intitule_entrainement'] . '</td>
<td>' . date("j/n/Y", strtotime($data['date_entrainement'])) . " " . date("G:i", strtotime($data['date_entrainement'])) . '</td>
<td><span class=" glyphicon glyphicon-plus" aria-hidden="true"></span></td>
</tr>
<tr class="exercicesHidden">
<td>1</td>
<td>' . $data['nom_exm'] . '</td>
<td>' . $data['reps'] . '</td>
<td>' . $data['poids'] . '</td>
<td>Up!</td>
</tr>
';
//echo '<tr class="exercicesHidden"><td>Coucou je suis caché!</td></tr>';
}
echo '
</tbody>
</table>
</div>'
I would like all the exercises are stored under one training, and when I click on it, it shows all the exercises from this training.
I hope I gave you all the things to help me to correct this problem!

Categories