I've never asked any question here before, but after spending many hours searching for a hint I decided to ask a question.
I have a project at school where I need to create a table from DB for all records for one user, and then create an expanded view for all details for a chosen record.
So far I have:
$user = 'myuser';
$pass = 'mypassword';
$db = new PDO( 'mysql:host=localhost;dbname=mydb', $user, $pass );
$sql = "
SELECT id,login
FROM quotes_taxi
WHERE login='[usr_login]'";
$query = $db->prepare( $sql );
$query->execute();
$results
where 'id' is obviously ID for each record. Then I create a table using FOREACH
<?php foreach( $results as $row ){
echo '<tr>';
echo '<td>'; echo $row['id']; echo '</td>';
echo '<td>'; echo $row['login']; echo '</td>';
echo '<td>'; echo ' View All ';
echo '</td>';
echo "</tr>";
}
?>
the problem I am having is: how can I attached that 'id' within the link (and it needs to be a separate button for each ID), so I can pass it to the view/index.php where I am using
$quote_id = $_GET["id"];
to get all other details of a chosen ID...
I know I have an error in that line, I just cannot figure it what. I also assume it is an easy problem, but I just cannot get my head around it
any help is most appreciated.
Thank you
Try to separate your PHP and HTML that way it will be easier to find errors quickly. Try the following code.
<table>
<tr>
<th>Id</th>
<th>Login</th>
<th>View</th>
</tr>
<?php foreach ($results as $row) : ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['login']; ?></td>
<td> View All </td>
</tr>
<?php endforeach; ?>
</table>
Output
I suggest this function
function AddGetParams()
{
$linkParams="";
foreach($GLOBALS["_GET"] as $key=>$value)
{
$linkParams.="$key=$value&";
}
return $linkParams;
}
Related
Hi i want to show all the data of one row in a table.But i can only show 1 column of the table.
function getAllRecipes(): array {
global $connection;
$query = "SELECT * FROM recipe";
$stmt = $connection->prepare($query);
$stmt->execute();
return $stmt->fetchAll();
}
$recipe = getAllRecipes();
<?php foreach ($recipe as $recip) : ?>
<table>
<tr>
<td><?= **$recip["id"];** ?></td> <----here
</tr>
</table>
This is the result
and i want all of the line:
Do you know how to do it ?
Thanks for your help
Based on "i just want by one line show all the contain of th row" in your "answer".
$arr_values = array_values($recipe);
$html = '<table><tr><th>' . implode('</th><th>', $arr_values) . '</tr></table>';
echo $html;
If you simply only want the name of the keys(columns):
$arr_keys = array_keys($recipe);
$html = '<table><tr><th>' . implode('</th><th>', $arr_keys) . '</tr></table>';
echo $html;
Hi there i am using a sqlite database and i have written a query in php that is running. However i cant seem to build a solution that will display the results of this query in the browser (tabular or other). Here is my code. This is the last element of this system so help is appreciated.
Code:
$db = new PDO('sqlite:daypilot.sqlite');
$start = '2018-02-20';
$end = '2018-02-25';
$sql = 'SELECT * FROM events WHERE end > ? AND start < ?';
$stmt = $db->prepare($sql); $stmt->execute([$start, $end]);
$events = $stmt->fetchAll();
[Update] From comment, OP has tried
<table>
<tr>
<th>id</th> <th>name</th> <th>contact</th>
</tr>
<?php foreach ($events as $event): ?>
<tr>
<td><?php echo $event['id'] ?></td>
<td><?php echo $event['name'] ?></td>
<td><?php echo $event['contact'] ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php
$db = new PDO('sqlite:daypilot.sqlite');
$start = '2018-02-20';
$end = '2018-02-25';
$sql = 'SELECT * FROM events WHERE end > ? AND start < ?';
$stmt = $db->prepare($sql); $stmt->execute([$start, $end]);
$events = $stmt->fetchAll(PDO::FETCH_ASSOC);
$table = '<table>';
foreach($events as $event) {
$table .= ' <tr>';
$table .= ' <td>' . $event['column_name'] . '</td>';
// repeat for every column you want to add
$table .= ' </tr>';
}
$table .= '</table>';
echo $table;
?>
Well, this is the simplest (and not the best) way to build the HTML table. You should insert that snippet code in the place where you want the table appears.
I passed PDO::FETCH_ASSOC to fetchAll method to ensure that the data are returned as associative array, then you have to iterate over it and extract the desired columns
I have been working on displaying the user database as a table in wordpress. Using get_results() i am getting my desired output also. But is there any workaround for this by passing arguments. Here is my work :
<?php
get_header(); ?>
<?php
global $wpdb;
$table_name = $wpdb->prefix .'users';
$row = $wpdb->get_results("select*from $table_name");
echo "<h2><center><u>List Of Members</u></center></h2>";
echo "<table class='table'>";
echo "<thead>
<tr>
<th><center>Member Name</center></th>
<th><center>Age</center></th>
<th><center>Class</center></th>
<th><center>Address</center></th>
</tr>
<thead>";
foreach ($row as $row)
{
$usid = $row->ID;
$username = $row->user_login;
echo "<tr><td>".$username."</td>";
$age=get_user_meta( $usid, 'age', true);
echo "<td>".$age."</td>";
$class=get_user_meta($usid, 'class', true);
echo "<td>".$class."</td>";
$address=get_user_meta($usid, 'address', true);
echo "<td>".$address."</td>";
}
?>
Yes there is. use get_users this article will help you https://codex.wordpress.org/Function_Reference/get_users
Essentially, I am creating a movie ordering system that has 7 movie genres, and I want each movie tab to populate according to genre. At this point I cant even get the table to show up. Can anyone help me out?
<div id="content">
<table>
<tr><th>Name</th> <th>Genre</th> <th>Year</th> <th>Rating</th></tr>
<?php
//connect to database
$dbc = mysql_connect('localhost' , 'username' , 'password');
$test = mysql_select_db('movies', $dbc);
if ($test = mysql_select_db('movies', $dbc))//test
{
$query = "SELECT * FROM 'movies' WHERE 'genre' = 'Action'";
//call query
$result = mysql_query($query, $dbc);
while ($row = mysql_fetch_array($result))
{
?>
<tr>
<td><?php print $row['name']; ?></td>
<td><?php print $row['genre']; ?></td>
<td><?php print $row['year']; ?></td>
<td><?php print $row['rating'];?></td>
</tr>
<table>
<?php
}//close the while loop
}//close the if statement
mysql_close($dbc);
?>
First of all, do not use mysql because of secure problems and it will be not suported in PHP later. Read about this http://php.net/manual/en/function.mysql-connect.php .
Try to use PDO or mysqli, for example :
$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM 'movies' WHERE 'genre' = 'Action'");
while($row = mysqli_fetch_array($result))
{
echo $row['name'];
echo "<br>";
}
mysqli_close($con);
I would like to have the freedom to place a row entry from my database wherever i' prefer in the page. Right now, the php code that I use is as follows (it is clean working code):
<html><head></head>
<body>
<?php
$db = mysql_connect("xxx","xxx","xxx") or die("Database Error");
mysql_select_db("caisafety",$db);
$id = $_GET['id'];
$id = mysql_real_escape_string($id);
$query = "SELECT * FROM `cert_rr` WHERE `id`='" . $id . "'";
$result = mysql_query($query);
echo $row['id']; while($row = mysql_fetch_array( $result )) {
echo "<br><br>";
echo $row['basic3'];
echo $row['basic2'];
echo $row['basic1'];
}
?>
</body>
</html>
I call id through the browser Eg. http://site.com/getid.php?id=10 . But I do not have the freedom to place my row entry within my html. For eg. like this:
<table><tr>
<td align="center">BASIC INFO 1: <?php echo $row['basic1']; ?></td>
<td align="center">BASIC INFO 2: <?php echo $row['basic2']; ?></td>
</tr></table>
I can place html within echo PHP tags but then I have to clean up my html and thats a lot of work. Retaining HTML formatting would be preferred. Any help or guidelines on this would be much appreciated.
<?php
$db = mysql_connect("xxx","xxx","xxx") or die("Database Error");
mysql_select_db("caisafety",$db);
$id = $_GET['id'];
$id = mysql_real_escape_string($id);
$query = "SELECT * FROM `cert_rr` WHERE `id`='" . $id . "'";
$result = mysql_query($query);
//you need to retrieve every row and save to an array for later access
for($rows = array(); $tmp = mysql_fetch_array($result);)
{
$rows[] = $tmp;
}
//now you can use the $rows array where every you want e.g. with the code from Zhube
?>
....
<table><?php foreach($rows as $r):
<td><?php echo $r['id'] ?></td><?php endforeach ?>
</table>
By
while($row = mysql_fetch_array( $result )) {
echo "<br><br>";
echo $row['basic3'];
echo $row['basic2'];
echo $row['basic1'];
}
you save only the last row
Instead of having your HTML tags as part of the PHP variable, do something like this instead:
<table><?php foreach($row as $r):?>
<td><?php echo $r['id'] ?></td><?php endforeach ?>
</table>