i have an array with multiple user id receiving from another page through header function
$Values= unserialize(urldecode($_GET['vals']));
this line give me such array
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 )
by using for each loop i am using each user id to retrieve data from database table and displaying into html table with following code
//displaying data into tables -->
$Values= unserialize(urldecode($_GET['vals']));
if(isset($Values))
{
foreach($Values as $user_id)
{
$prevCon['where'] = $user_id;
$prevUser = $user->getRowss($prevCon);
while($row = $prevUser->fetch_assoc())
{
echo "<tr>";
echo "<td>" . $row['full_name'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['country_name'] . "</td>";
echo "<td>" . $row['city_name'] . "</td>";
echo "<td>" . $row['NIC'] . "</td>";
echo "</tr>";
}
}
}
else
{
echo " no data to display";
}
but my loop runs for 36 time each entry against one user id is being display for 6 times and then another user id is used again for 6 times
please do help me i want to display one entry for each user id
Related
From the research i conducted in SO, this is the only other question similar to what i need, but im afraid that i didnt help me (How to replace null results in sql query result table with a string?).
I have a table that prints the Medical History of a Patient, and the table cells are restored from a database.
A portion of the code is down below:
echo "<tr>";
echo "<th>MRI Onset Localisation</th>";
echo "<th>CNS MRI Lesions Y/N </th>";
echo "<th>CNS MRI Lesions No.</th>";
echo "<th>CNS MRI Location</th>";
echo "<th>Person Signing the form</th>";
echo "<th>Documented at</th>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row['Onsetlocalisation'] . "</td>";
echo "<td class='tdclass exempt'>" . $row['smoker'] . '<br>' . $row['cigars'] . '<br>' . $row['cigardate'] . "</td>";
echo "<td>" . $row['onsetsymptoms'] . "</td>";
echo "<td class='tdclass exempt'>" . $row['MRIonsetlocalisation'] . "</td>";
echo "<td>" . $row['MRIenhancing'] . "</td>";
echo "<td class='tdclass exempt'>" . $row['MRInum'] . "</td>";
echo "<td>" . $row['MRIenhancinglocation'] . "</td>";
echo "<td class='tdclass exempt'>" . $row['signer'] . "</td>";
echo "<td>" . $row['reg_date'] . "</td>";
echo "</tr>";
And the MySQL Query that returns those fields is
$sql = "SELECT * FROM tblName WHERE somefield = $somevar";
The thing is... Some of those fields are allowed to have NULL as a value, but for the front end of the app, i want the table cells that contain NULL values to print a standard string such as "N/A" or "Empty" but i need it to be done inside of the php table portion of my code, or so i think...
I found numerous questions about replacing NULL with a string inside of the MySQL query, but i don't care about that since the query doesn't appear at the end user outside of the table.
I tried to implement the coalescing operator in the following way, but no nothing was displayed:
echo "<td class='tdclass exempt'>" . $row['MRInum'] ?? "N/A" . "</td>";
Any help is absolutely welcome.
You need to put the expression in brackets to make it clear which parts are supposed to be evaluated by the null coalescing operator:
echo "<td class='tdclass exempt'>" . ($row['MRInum'] ?? "N/A") . "</td>";
If you don't, it thinks the "<td class='tdclass exempt'>" is part of the text to be checked for existence - which means it always returns true because that hard-coded text always exists.
You can do this in two ways.
From SQL:
COALESCE(fieldName, 'N/A') AS fieldName
will replace NULLs with, you guessed it, 'N/A'.
Otherwise, when you fetch the row,
$row = $stmt->fetch(PDO::FETCH_OBJECT);
you can vet it:
$replacements = [
'MRI_Data' => 'N/A',
'MRI_Type' => [
'001' => 'Single Scan',
'002' => 'Multi Scan',
'003' => 'Pulsed',
'(null)' => 'N/A',
],
];
foreach ($row as $key => $value) {
if (array_key_exists($key, $replacements)) {
if (is_array($replacement[$key])) {
if (empty($value)) {
$value = '(null)';
}
if (array_key_exists($value, $replacement[$key])) {
$row[$key] = $replacement[$key][$value];
}
} else {
if (empty($value)) {
$row[$key] = $replacement[$key];
}
}
}
}
This question already has answers here:
How to extract and access data from JSON with PHP?
(1 answer)
Read JSON Data Using PHP [duplicate]
(5 answers)
Closed 4 years ago.
I have a JSON file in this format I want to data display in the table using for loops and for each loop what can I do?
[{"fertilizer":
{"pg1":"-21.259515860749435","pg2":"24.741169305724725","lastyearlastmonth":"764.119","currentmonth":"601.671","currentyearytd":"5735.1","lastyearytd":"4597.6","pname":"Urea","mmonth":"11","period":"11","myear":"2017"}},{"fertilizer":{"pg1":"-20.53085432388131","pg2":"9.258986807905458","lastyearlastmonth":"631.435","currentmonth":"501.796","currentyearytd":"2227.9","lastyearytd":"2039.1","pname":"DAP","mmonth":"11","period":"11","myear":"2017"}},{"fertilizer":{"pg1":"67.37546062508531","pg2":"51.07126222636238","lastyearlastmonth":"36.635","currentmonth":"61.318","currentyearytd":"648.7","lastyearytd":"429.4","pname":"CAN","mmonth":"11","period":"11","myear":"2017"}}]
i want to display in table through loop but i have problem how can i display all data thrugh using loops
foreach ($data as $nt)
{
echo "<tr class='{$dispval} {$boldrow}' >";
echo "<td>{$nt[pname]}</td>";
echo "<td class='txtright'>" . number_format($nt[lastyearlastmonth],1) . " </td>";
echo "<td class='txtright'>" . number_format($nt[currentmonth],1) . " </td>";
echo "<td class='txtright'>" . number_format($nt[pg1],1) . " </td>";
echo "<td class='txtright'>" . number_format($nt[lastyearytd],1) . " </td>";
echo "<td class='txtright' >" . number_format($nt[currentyearytd],1) . " </td>";
echo "<td class='txtright'>" . number_format($nt[pg2],1) . " </td>";
echo "</tr>";
}
You are on the right way!
First of all, json is a plain string in PHP. You need to decode it first using: json_decode();
For example:
$items = json_decode('your json string here');
foreach($items as $item) {
echo "<tr>";
echo "<td>".$item->fertilizer->pg1."</td>";
echo "<td>".$item->fertilizer->pg2."</td>";
// etc
echo "</tr>";
}
Ive got a pretty basic table named 'customers' with four columns:
ID (primary Auto Increment)
businessName
contactName
contactEmail
I call it with:
$result = mysqli_query($con, "SELECT * FROM customers");
and was using mysqli_fetch_array to display it on the page with a foreach loop:
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
echo "<tr>";
foreach ($row as $value) {
echo "<td>" . $value . "</td>";
}
echo "<td><a href='updateform.php?id=" . $row['id'] . "'>Edit</a></td>";
echo "</tr>";
}
Which gives you an array like:
Array ( [id] => 1 [businessName] => Microsoft [contactName] => Bill Gates [contactEmail] => bill#microsoft.com ) Array ( [id] => 2 [businessName] => Amazon [contactName] => Jeff Bezos [contactEmail] => jeff#amazon.com )
Is it possible to display results differently based on which column (technically now position in the array) they are in? I would like to make a
a href="mailto:bill#microsoft.com"
link when it gets to contactEmail.
What if I wanted to display just one key or value instead of using foreach?
It doesn't seem possible to call
$row['contactEmail']
in the foreach loop
which confuses me since below that I am able to create a link to
$row['id']
If anyone has any ideas how to do this, or if there is a better way to be displaying this information, I'm open to suggestions, as I'm not very good at programming, especially PHP.
Yes! you can just add like this:
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
echo "<tr>";
foreach ($row as $key => $value) {
if($key == "contactEmail"){
$mailUrl = "mailto:".$value;
echo "<td>".$value."";
}
else{
echo "<td>" . $value . "</td>";
}
}
echo "<td><a href='updateform.php?id=" . $row['id'] . "'>Edit</a></td>";
echo "</tr>";
}
Yes, You can able to mysqli_fetch_array retrieve data directly using foreach loop. like bellow :
<?php
$result = mysqli_query($con, "SELECT * FROM customers");
?>
<table>
<tr>
<td>Business Name</td>
<td>Contact Name</td>
<td>#</td>
</tr>
<?php
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
echo "<tr>";
echo "<td>".$row['businessName']."</td>";
foreach ($row as $key => $value) {
$newValue = $key == "contactEmail" ? ''.$value.'' : $value;
echo "<td>" . $newValue . "</td>";
}
echo "<td><a href='updateform.php?id=" . $row['id'] . "'>Edit</a></td>";
echo "</tr>";
}
?>
</table>
I'm just getting into php, html etc and am trying to resolve an issue at work. Please be patient with my lack of experience.
I have a csv file, TEST2.csv. it is as follows advisor,guest,stockNumber,part,qty,partnote1,partnote2,status that I am putting on a webpage so that our staff can select their advisor number and see the status of their parts. The report we export from our system is in csv and has columns that I do not want to show on this report.
(before I hear I should learn another format to display this, I know, Im under a time constraint and need something quick and rudimentary for now)
Everything works well but it does not show column 7, only up to 4. I want it to skip 5,6 Here's the monster i've created:
<table id="demo">
<tbody>
<?php
$fp = fopen ( "TEST2.csv" , "r" );
$wantedColumns = array(0,1,2,3,4,7);
while (( $data = fgetcsv ( $fp , 1000 , "," )) !== FALSE ) {
$i = 0;
echo "<tr>";
foreach($data as $row) {
if (!in_array($i,$wantedColumns)) continue;
echo "<td>" . $row . "</td>";
$i++ ;
}
echo "/<tr>";
}
fclose ( $fp );
?>
`
Any help would be greatly appreciated!
Using simple array syntax with [index] your code will be:
$fp = fopen ( "TEST2.csv" , "r" );
while (( $data = fgetcsv ( $fp , 1000 , "," )) !== FALSE ) {
echo "<tr>";
echo "<td>" . $data[0] . "</td>";
echo "<td>" . $data[1] . "</td>";
echo "<td>" . $data[2] . "</td>";
echo "<td>" . $data[3] . "</td>";
echo "<td>" . $data[4] . "</td>";
echo "<td>" . $data[7] . "</td>";
echo "</tr>";
}
fclose ( $fp );
This is assignment so not looking for anything perfectly safe and secure, just working.I have table in SQL database, I'm printing down all records, all records have unique reference number, for each row of the database I printed down I gave checkbox with value of the row's ref. number, when I submit them by "POST" everything is working and printing out:
if (!$_POST['checkbox']) {
echo "Your basket is empty.";
} else {
echo "<table border='0' id='games_table' cellspacing='1'>";
echo "<tr id='basket_table_row'>";
echo "<td colspan='3'>" . "Logged: " . $_SESSION['user'] . "</td>";
echo "<td colspan ='2'>" . "OS used on this machine: " . "<script type='text/javascript'>document.write(yourOS())</script><noscript>Computers</noscript>" . "</td>";
echo "</tr>";
echo "<tr id='basket_table_row'>";
echo "<td colspan='5'>" . "You put into the basket these games: " . "</td>";
echo "</tr>";
foreach ($_POST['checkbox'] as $value) {
$_SESSION['basket']=array($value);
$res=pg_query($conn,"select * from CSGames where refnumber='$value'");
while ($a = pg_fetch_array ($res)) {
echo "<tr id='games_table_row'>";
echo "<td>" . $a["refnumber"] . "</td>";
echo "<td>" . $a["title"] . "</td>";
echo "<td>" . $a["platform"] . "</td>";
echo "<td>" . $a["description"] . "</td>";
echo "<td>" . $a["price"] . "</td>";
echo "</tr>";
}
}
echo "</table>\n";
}
but only think which stays recorded in $_SESSION['basket'] is value of the last checkbox but I need all of them (60 or 70).
what Am I doing wrong?
You are overwriting te value of $_SESSION['basket'] at each iteration of the loop.
The last value is the only one stored.
Currently, you are only storing the last value, if you wish to store every value, you should add it like this:
$_SESSION['basket'][] = $value;
foreach($_POST['checkbox'] as $value){
$_SESSION['basket']=array($value);
}
every iteration of your loop is overwriting the value in $_SESSION['basket'], hence you only seeing the last checkbox value.
In every step of foreach, you create a new array in $_SESSION['basket'] with only one item, the current value of $value. It is corrected like this:
// ...
$_SESSION['basket'] = array();
foreach ($_POST['checkbox'] as $value) {
$_SESSION['basket'][] = $value;
// ...
}
// ...
You can directly do it like this
$_SESSION['basket'] = $_POST['checkbox'];
because the data of $_POST['checkbox'] is an array anyway.
What you are doing is looping the $_POST['checkbox'] and saving each data as array to a session.
this $_SESSION['basket'] = $_POST['checkbox'];
and
foreach ($_POST['checkbox'] as $value) {
$_SESSION['basket'][]=$value;
}
will have the same value.
What you have right only saves the last value of $_POST['checkbox']