How to change column name into a variable - php

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?

Related

Lightbox not displaying images when using MySQL and PHPMyAdmin

I have encountered a small problem when trying to make my website more advanced. To put it simple:
when I wanted to display images in a table using Lightbox and PHP everyting worked well:
<tr>
<td class="w-25">
<a href="telewizory/telewizor1.jpeg" data-toggle="lightbox" data-lightbox="telewizor1" class="col-sm-4" >
<img src="telewizory/telewizor1.jpeg" class="img-fluid img-thumbnail" alt="Sheep">
</td>
<td>Philips 70PUS6704/12</td>
<td>3999</td>
</tr>
The first column presented the image, the second - name and the last one - price.
how it works
However, when I wanted to add MySQL and created a database in PHPMyAdmin something went wrong. I can still read the name and the price well, but images don't show, I can only see the alternative text - Sheep in this example.
New code:
<?php
$kat_id = isset($_GET['kat_id']) ? (int)$_GET['kat_id'] : 1;
$sql = 'SELECT `img`, `nazwa` , `cena`
FROM `produkty`
WHERE `kategoria_id` = ' . $kat_id .
' ORDER BY `nazwa`';
$wynik = mysqli_query($polaczenie, $sql);
if (mysqli_num_rows($wynik) > 0) {
while ($produkt = #mysqli_fetch_array($wynik)) {
echo
'<tr>
<td class="w-25">
<a href="telewizory/' . $produkt['img'] . ' data-toggle="lightbox" data-lightbox="' . substr($produkt['img'], 0, strpos($produkt['img'], ".")) . '" class="col-sm-4">
<img src="telewizory/' . $produkt['img'] . ' class="img-fluid img-thumbnail" alt="Sheep">'
. ' </td>
<td>' . $produkt['nazwa'] . '</td>'
. '<td>' . $produkt['cena'] . '</td>
</tr>'
. PHP_EOL;
}
} else {
echo 'wyników 0';
}
mysqli_close($polaczenie);
?>
how it doesn't work
I have no idea what may be the reason that this code doesn't work. I would be very grateful for any help :)
You should check that the $produkt['img'] here
<img src="telewizory/' . $produkt['img'] . ' class="img-fluid img-thumbnail" alt="Sheep">'
. ' </td>
is replaced with the expected value from the DB.

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

Database rows not displaying fully in HTML table

I am attempting to display mySQL data in a HTML table through php. The first row is displaying correctly, however the other row sets are not being organised and displayed in my table, rather just echoing out at the bottom of the container with no structure.
I think that since the data is being displayed, albeit not in the table, that my query is correct, im just unsure on how to proceed.
Is this an issue with my table structure?
I tried adding a second:
echo '<td>' . $data['studentNumber'] . '</td><td>' . $data['handle'] . '</td><td>' . $data['email'] . '</td>';
underneath my first echo, but it just duplicated everything.
Here is the entire code in question: Screenshot Here
<div class="container-fluid">
<div class="row">
<div class="col-md details">
<p class="details_title"></p>
<?php
$getAllStudentsTable = "SELECT * FROM users WHERE accessLevel = 3";
$result = (mysqli_query($conn, $getAllStudentsTable));
echo '<table class="table">
<thead class="thead-dark">';
echo' <tr>
<th scope="col">Student Number</th>
<th scope="col">Handle</th>
<th scope="col">Email Address</th>
</tr>
</thead>'; //table headers
while ($data = mysqli_fetch_array($result)) {
echo' <tbody> <tr>';
echo '<td>' . $data['studentNumber'] . '</td><td>' . $data['handle'] . '</td><td>' . $data['email'] . '</td>';
echo'</tr> </tbody> </table>';
}
?>
Can anyone point me in the right direction on this?
(I am relatively new to PHP, SO and this is the first attempt ever at trying to display database data into an HTML table.)
I have attached a link for a screenshot of the issue (Not yet allowed to post pictures lol).
Thanks!
You're echoing most of the table structure in your loop, where you should just be echoding the rows. As a necessary debugging step, take a look at the View Source in your browser and see what the table structure is. You'll find multiple <tbody> elements and multiple closing </table> tags, confusing the browser.
Basically, remove the various <tbody> and <table> tags from your loop and just echo them around the loop. Something like this:
echo '<tbody>';
while ($data = mysqli_fetch_array($result)) {
echo '<tr>';
echo '<td>' . $data['studentNumber'] . '</td><td>' . $data['handle'] . '</td><td>' . $data['email'] . '</td>';
echo '</tr>';
}
echo '</tbody></table>';
All you want to repeat in the loop is each <tr> element and its children.

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!

trying to update mysql database in php

I'm trying to have an html form which updates mysql data. Now , I have this code(which is also a form action) and I'm trying to also use this as a form for my update. Because I will need the data that this form would show, so that it will be easier for the users to update only what they wish to update.
this is the form that will try to search the data :
<form name="form1" method="post" action="new.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="16" style="background:#9ACD32; color:white; border:white 1px solid;
text-align: center"><strong><font size="3">ADMISSION INFORMATION SHEET</strong></td>
</tr>
<tr>
This is new.php( will display the corresponding data based on the firstname inputted. And will also try to serve as a form for the update process.
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("Hospital", $con);
$result = mysql_query("SELECT * FROM t2 WHERE FIRSTNAME='{$_POST["fname"]}'");
?>
<table width="900" border="0" align="left" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="16" style="background:#9ACD32; color:white; border:white 1px solid; text-align: center"><strong><font size="3">ADMISSION INFORMATION SHEET</strong></td>
</tr>
<tr>
<?php while ( $row = mysql_fetch_array($result) ) { ?>
<form name="form1" method="post" action="update.php">
<td width="54"><font size="3">Hospital #</td>
<td width="3">:</td>
<td width="168"><input name="hnum" type="text" value="<?php echo $row["HOSPNUM"]; ?>">
</td>
This is my update.php,
mysql_select_db("Hospital", $con);
mysql_query("UPDATE t2 SET HOSPNUM='$_POST[hnum]' ROOMNUM='$_POST[rnum]',
LASTNAME='$_POST[lname]', FIRSTNAME='$_POST[fname]', MIDNAME='$_POST[mname]',
CSTAT='$_POST[cs]' AGE='$_POST[age]', BDAY='$_POST[bday]', ADDRESS='$_POST[ad]',
STAT='$_POST[stats1]', STAT2'$_POST[stats2]', STAT3'$_POST[stats3]',
STAT4'$_POST[stats4]', STAT5'$_POST[stats5]', STAT6'$_POST[stats6]',
STAT7'$_POST[stats7]', STAT8'$_POST[stats8]', NURSE='$_POST[nurse]', TELNUM
='$_POST[telnum]'
WHERE FNAME ='$_POST[fname]'");
mysql_close($con);
?>
-Please help, I don't have any idea why it isnt updating the data.
Typo, there is a missing "," between HOSPNUM and ROOMNUM:
SET HOSPNUM='$_POST[hnum]', ROOMNUM=
The previous comments are absolutely correct. I would recommend using the PDO or MySQLi adapters and use a prepared statement for your record insertion as a bare minimum of security. Using the first name as a unique identifier is a bad idea. Don't you have a primary key column in the table?
To answer your actual question, one the problem is with the array notation in the double-quoted string. There are several equals signs missing from your statement as well. Try this:
mysql_query("
UPDATE t2
SET HOSPNUM='" . mysql_real_escape_string($_POST['hnum']) . "',
ROOMNUM='" . mysql_real_escape_string($_POST['rnum']) . "',
LASTNAME='" . mysql_real_escape_string($_POST['lname']) . "',
FIRSTNAME='" . mysql_real_escape_string($_POST['fname']) . "',
MIDNAME='" . mysql_real_escape_string($_POST['mname']) . "',
CSTAT='" . mysql_real_escape_string($_POST['cs']) . "',
AGE='" . mysql_real_escape_string($_POST['age']) . "',
BDAY='" . mysql_real_escape_string($_POST['bday']) . "',
ADDRESS='" . mysql_real_escape_string($_POST['ad']) . "',
STAT='" . mysql_real_escape_string($_POST['stats1']) . "',
STAT2='" . mysql_real_escape_string($_POST['stats2']) . "',
STAT3='" . mysql_real_escape_string($_POST['stats3']) . "',
STAT4='" . mysql_real_escape_string($_POST['stats4']) . "',
STAT5='" . mysql_real_escape_string($_POST['stats5']) . "',
STAT6='" . mysql_real_escape_string($_POST['stats6']) . "',
STAT7='" . mysql_real_escape_string($_POST['stats7']) . "',
STAT8='" . mysql_real_escape_string($_POST['stats8']) . "',
NURSE='" . mysql_real_escape_string($_POST['nurse']) . "',
TELNUM='" . mysql_real_escape_string($_POST['telnum']) . "'
WHERE FNAME='" . mysql_real_escape_string($_POST['fname']) . "'
");

Categories