Echo substring problem in parantheses with html [duplicate] - php

This question already has answers here:
Show a number to two decimal places
(25 answers)
Closed 2 years ago.
<?php
echo ' <tr>
<td>John Doe</td>
<td>34</td>
<td>44</td>
<td>' .$ks1. '</td>
<td>' .$ks1. '</td>
<td>' . ($ks2 - $ks1) . '</td>
<td><div class="progress progress-lg">
<div class="progress-bar" data-progress="' . ($ks1*100 / $ks2) . '"></div>
</div></td>'; ?>
When I run this page, progress bar shows 75.75757575757. I want to get first 4 digit.
I try to add substr but it gives error.
<div class="progress-bar" data-progress="'substr(. ($ks2*100 / $ks1) .,1,4);'"></div>
</div></td>'; ?>
How can i fix it?
Thanks.

I would use round() in this case:
<?php
echo '<tr>
<td>John Doe</td>
<td>34</td>
<td>44</td>
<td>' .$ks1. '</td>
<td>' .$ks1. '</td>
<td>' . ($ks2 - $ks1) . '</td>
<td><div class="progress progress-lg">
<div class="progress-bar" data-progress="' . round(($ks1 * 100 / $ks2), 2) . '"></div>
</div></td>';
?>
This will return 75.76 because of the rounding. If you still want 75.75 you can use substr(), but that will only work if the number is between 10 and 99 whereas round() will always return 2 decimals (at most).

Related

How to remake the following construction so that the variables variable_1 and variable_2 are hidden (remove them from the url)? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 days ago.
Improve this question
How to remake the following construction so that the variables variable_1 and variable_2 are hidden (remove them from the url)?
$stmt = pg_query( $conn, "select * from tables");
while ($row = pg_fetch_array($stmt)) {
$i=$i+1;
echo '
<td class="table__text">' . substr($i, 0, 30) . '</td>
<td class="table__text"><a target="_blank" class="link" href=/www/html/index.php?variable_1='.$row[0].'&variable_2='.$row[2].'>'.$row[0].'</a></td>
<td class="table__text">' . substr($row[1], 0, 30) . '</td>
<td class="table__text">' . substr($row[2], 0, 30) . '</td>
<td class="table__text">' . substr($row[3], 0, 300) . '</td>
<td class="table__text">' . substr($row[4], 0, 300) . '</td>
</tr>';
}
I tried to remake it in POST, but the last value from the select is substituted into the variables.
$stmt = pg_query( $conn, "select * from tables");
while ($row = pg_fetch_array($stmt)) {
$i=$i+1;
echo '
<td class="table__text">' . substr($i, 0, 30) . '</td>
<td class="table__text">
<form target="_blank" action="/www/html/index.php" method="POST">
<input type="hidden" name="variable_1" value="'.$row[0].'">
<input type="hidden" name="variable_2" value="'.$row[2].'">
<input type="submit" value="'.$row[0].'">
</from></td>
<td class="table__text">' . substr($row[1], 0, 30) . '</td>
<td class="table__text">' . substr($row[2], 0, 30) . '</td>
<td class="table__text">' . substr($row[3], 0, 300) . '</td>
<td class="table__text">' . substr($row[4], 0, 300) . '</td>
</tr>';
}

Data not Displayed after Submit Post [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 2 years ago.
I have a problem with my project and i don't know to where is the bug.
Problem: After i Submit Form data is not fetch in the Page.
I tried var_dump($result) but is not return data.
I will Expose my Code and i hope you can help me or guide me to resolve the problem.
<?php
include "connect.php";
include 'header.php';
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
if (isset($_POST["Search"])) {
$table_name = 'imp_acc';
$query = "
SELECT * FROM '.$table_name.' WHERE available = 'YES'
";
if (isset($_POST["regi"], $_POST["stat"])) {
$query .= "
AND reg " . $_POST["regi"] . " AND stat " . $_POST["stat"] . "
";
}
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
$total_row = $statement->rowCount();
$output = '';
if($total_row > 0)
{
$output .= '
<table class="table">
<thead>
<tr>
<th scope="col">RRG</th>
<th scope="col">LV</th>
<th scope="col">CR</th>
<th scope="col">PR</th>
<th scope="col">FR</th>
<th scope="col">ES</th>
<th scope="col">RO</th>
<th scope="col">BO</th>
<th scope="col">CP</th>
<th scope="col">SK</th>
<th scope="col">LP</th>
<th scope="col">PRI</th>
<th scope="col">Action</th>
<th scope="col"><th>
<th scope="col"></th>
</tr>
</thead>
';
foreach($result as $row)
{
$timestamp = strtotime($row['lap']);
$newformat = date('Y-m-d',$timestamp);
//$product_id = $row["id"];
$output .= '
<tr>
<td>' . $row["regi"] . '</td>
<td>' . $row["lv"] . '</td>
<td>' . $row["crr_rr"] . '</td>
<td>' . $row["prr_rr"] . '</td>
<td>' . $row["frr_rr"] . '</td>
<td>' . $row["stat"] . '</td>
<td>' . $row["b_o"] . '</td>
<td>' . $row["r_o"] . '</td>
<td>' . $row["cc_count"] . '</td>
<td>' . $row["sk_count"] . '</td>
<td>' . $newformat . '</td>
<td>' . $row["pri_cc"] . '</td>
</tr>
';
}
$output .= ' </table>';
}
else
{
$output = '<h3>No Data Found</h3>';
}
echo $output;
}
?>
Also i need your Opinion for finish my Projects.
I have a Search Page with alot of filters options.
Couple of them are not mandatory , are just optional.
Every filter have a different value in form.
I want to continue in this way to makes more POST like this:
if (isset($_POST["regi"], $_POST["stat"] , ....)
Or i can give a chance to Switch statement, to see how it works.
I wait your opinions and ideea.
Thank you for help
I find the error on code.
It was my fault because i using value and not the form name.
In form data i have something like this:
submit: "Search"
and the code need to be like this:
if (isset($_POST["submit"]))
Now i see another problem is my $result because it comes array.
Thank you for your tips and help.

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

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