I am using a class that fetches information from a database and shows that information in a table, along with an edit and delete button. When I click the edit or delete button, I want to add $_GET['id'] to the link, but I am confused on how to accomplish this.
public function select($tName,$from,$where=1){
$key=[];
$con=$this->con;
$sql="SELECT $tName FROM $from WHERE $where";
$data = $con->query($sql);
$fullRec=[];
foreach($data as $k=>$rows){
$fullRec[]=$rows;
}
foreach($rows as $k=>$v){
$key[] = $k;
}
//create table
echo "<table border='1'>";
//create one row with col name selected!!
echo "<tr>";
for($z=0;$z<count($key);$z+=2){
echo "<td>".$key[$z]."</td>";
}
echo "<td>actions</td>";
echo "</tr>";
//create one row for each record comes!!
for($i=0;$i<count($fullRec);$i++){
echo "<tr>";
//create one table data for each record comes!!
for($j=0;$j<count($fullRec[$i])/2;$j++){
echo "<td>".$fullRec[$i][$j]."</td>";
}
echo "<td>EditDelete</td>";
echo "</tr>";
}
echo "</table>";
}
use like this to pass the id of the record in get parameter
echo "<a href='your_route?del=".$rows['id']."'>DELETE</a>";
Related
I have a MySQL table field which consists of a URL pointing towards an image. I'm using php and want the image to display in a table along with the other fields.
I think I need to use the PHP Switch statement and reference either the Row('Name') or Field Ref Number to create the code. I have this working fine in classic-asp, but need to code this in php.
$result = mysql_query("SELECT sometext,urlvalue,somemoretext FROM Table");
if (!$result) {
die("Query to show fields from table failed");
// Table fields
// sometext
// urlvalue an http image address such as http://www.example.com/image.jpg"
// somemoretext
$fields_num = mysql_num_fields($result);
echo "<table border='1'>
<tr bgcolor='yellow' align='left'>";
for($i=0; $i<$fields_num; $i++) {
$field = mysql_fetch_field($result);
echo "<th>{$field->name}</th>";
}
echo "\n";
// printing table rows
while($row = mysql_fetch_row($result)) {
echo "<tr>";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
switch $fields_num {
case "2":
// result cell looks like this ---->
// <td><a target='_blank' href='http://www.example.com/image.jpg'><img src='http://http://www.example.com/image.jpg'></a></td>
echo "<td><a target='_blank' href='" . $cell ."'><img src='" . $cell ."'></a></td>";
break;
default:
echo "<td>$cell</td>";
echo "</tr>\n";
}
mysql_free_result($result);
If you know the columns that you are going to be accessing and you know which column is the url, then you can use the following mysqli_-based code block with a condition in the inner loop to check for the matching column name.
If you don't know the columns that you will be dealing with, you can use filter_var($val, FILTER_VALIDATE_URL) assuming you only have one url string in your resultset. If you go for this second option, the one side effect on my code block is that you would have re-engineer the <th> portion.
I refuse to write answers using mysql_ functions, so please take this chance to modernize your code. I've included the connection declaration and basic error checking for your debugging convenience. (Never display error messages on your public site.)
Untested Code:
$columns = ['sometext', 'urlvalue', 'somemoretext'];
if (!$mysqli = new mysqli("localhost", "my_user", "my_password", "my_db")) {
echo "Database Connect Error: " , $mysqli->connect_error;
} elseif (!$result = $mysqli->query("SELECT `" . implode('`, `', $columns) . "` FROM `Table`")) {
echo "Query Syntax Error: " , $mysqli->error;
if (!$result->num_rows) {
echo "No Rows Found";
} else {
echo "<table border='1'>";
echo "<tr bgcolor='yellow' align='left'>";
echo "<th>" , implode("</th><th>", $columns) , "</th>";
echo "</tr>";
while ($row = $result->fetch_assoc()) {
echo "<tr>";
foreach ($row as $col => $val) {
echo "<td>";
if ($col == 'urlvalue') { // or if (filter_var($val, FILTER_VALIDATE_URL)) {
echo "<a target='_blank' href='$val'><img src='$val'></a>";
} else {
echo "$val";
}
echo "</td>";
}
echo "</tr>";
}
echo "</table>";
}
}
I am not completely sure what you mean but rather than referencing the field number just reference the name.
if ($result->num_rows > 0) {
echo "<table border='1'><tr bgcolor='yellow' align='left'>";
while($row = $result->fetch_assoc()) {
echo "<th>".$row['field-name']."</th>";
echo "<tr>";
echo "<td><a target='_blank' href='" . $row['field-name'] ."'><img src='" . $row['field-name'] ."'>
</a>
</td>";
echo "</tr>";
}
echo "</table>";
}
I have a table in database and I want to fetch the data from it. I know how to fetch data from database using php. But the problem is that
this table is different in structure. For example
and so on...
As you can see this table is like a pivot table. That is the real problem for me. All the entries of a single person should be in single row. But it is in different rows and single column (all entries of single person is in value column).
I have tried to fetch table with regular way like this
<?php
/*
Template Name: Registration
*/
get_header();
$results = $wpdb->get_results( "SELECT * FROM wp_cf_form_entry_values", ARRAY_A );
$count = $results->num_rows;
if(!empty($results)) {
echo "<table width='100%' border='1' cellspacing='1'>";
echo "<tbody>";
foreach($results as $row){
echo "<tr>";
echo "<td><center>" . $row['value'] . "</center></td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
} else {
echo "<table width='100%' border='1' cellspacing='1'><tr><td>No Records Found</td></tr></table>";
}
get_footer();?>
But with the above code it's showing all the results in a single column and that's what I expect from above code.
So I don't know the correct code to represent this table as single row entries of single person. I searched a lot but didn't get much.
I'm pretty sure you looking for something like this:
horizontal representation: (by Person)
<?php
/*
Template Name: Registration
*/
get_header();
global $wpdb;
//Create a mulit dimensional array for each person.
$persons = array();
$fields = $wpdb->get_results( 'SELECT * FROM wp_cf_form_entry_values');
foreach($fields as $field){
$persons[$field->entry_id][$field->slug] = $field->value;
}
//the table
echo '<table width="100%" border="1" cellspacing="1">';
$i=0;
foreach($persons as $id=>$values){
//the table header (only in the first loop)
if($i==0){
echo '<tr>';
foreach($values as $key=>$val){
echo '<th>'.$key.'</th>';
}
echo '</tr>';
}
// one line per person
echo '<tr id="'.$id.'">';
foreach($values as $key=>$val){
echo '<td>'.$val.'</td>';
}
echo '</tr>';
$i++;
}
if($i==0){
echo "<table width='100%' border='1' cellspacing='1'><tr><td>No Records Found</td></tr></table>";
}
echo '</table>';
get_footer();?>
vertical representation (by field slug)
<?php
/*
Template Name: Registration
*/
get_header();
global $wpdb;
//Create a mulit dimensional array for each slug.
$field_slugs = array();
$fields = $wpdb->get_results( 'SELECT * FROM wp_cf_form_entry_values');
foreach($fields as $field){
$field_slugs[$field->slug][$field->entry_id] = $field->value;
}
//the table
echo '<table width="100%" border="1" cellspacing="1">';
$i=0;
foreach($field_slugs as $slug=>$values){
//the table header (only in the first loop)
if($i==0){
echo '<tr>';
foreach($values as $person_id=>$val){
echo '<th></th>';
echo '<th>Person '.$person_id.'</th>';
}
echo '</tr>';
}
// one line per person
echo '<tr id="'.$slug.'">';
echo '<td><b>'.$slug.'</b></td>';
foreach($values as $person_id=>$val){
echo '<td>'.$val.'</td>';
}
echo '</tr>';
$i++;
}
if($i==0){
echo "<table width='100%' border='1' cellspacing='1'><tr><td>No Records Found</td></tr></table>";
}
echo '</table>';
get_footer();?>
You use foreign key entries_id so you can retrieve from that table ie
$results=$wpdb->get_results("select * from entries_table");
foreach($results as $result){
$entries_id=$result->id;
$entry_detail=$wpdb->get_results(SELECT * from form_field_table where entries_id='$entries_id' ");
foreach($entry_detail as $ent_detail){?>
<td><?php echo $ent_detial->slug;?></td><td><?php echo $ent_detial->value;?></td>
<?php }
}
I am currently working on a PHP and SQLite application. It does have HTML for the webpages being created. I am currently using PDO to connect the database to my online server. When I get to the webpage that allows me to type in what I am searching for then it will display what I have found in the echo statements below. I want to be able to have just the item name, that acts as a hyperlink; when it is clicked on it will go to another webpage (I believe) that will display the item's name, amount, and a short description. Is there a way to use PHP for this action or should I go with the HTML tagging?
if($_POST && isset($_POST['search'])) {
echo "<br>\n";
$query = $mysql->prepare('SELECT * FROM Items WHERE Name = :partname');
$subst = array ('partname' => $_POST['search']);
$query->execute($subst);
echo "<TABLE>";
echo "<tr>";
echo "<td>Name</td>";
echo "<td>Amount</td>";
echo "<td>Detail</td>";
echo "</tr>";
while ($row = $query->fetch()) {
//print_r($row);
echo "<tr>";
echo "<td>$row[Name]</td>";
echo "<td>$row[Amount]</td>";
echo "<td>$row[Detail]</td>";
echo "</tr>";
}
echo "</TABLE>";
} else echo "Item searched for was not found.";
from what i understand
if($_POST && isset($_POST['search'])) {
echo "<br>\n";
$query = $mysql->prepare('SELECT * FROM Items WHERE Name = :partname');
$subst = array ('partname' => $_POST['search']);
$query->execute($subst);
echo "<TABLE>";
echo "<tr>";
echo "<td>Name</td>";
echo "</tr>";
while ($row = $query->fetch()) {
//print_r($row);
$name = $row['Name'];
echo "<tr>";
echo "<td><a href='details.php?name={$name}'>{$name}</a></td>";
echo "</tr>";
}
echo "</TABLE>";
} else echo "Item searched for was not found.";
To use Associative Arrays within double quotes, you need to use curly braces:
echo "<td>{$row['Name']}</td><td>{$row['Amount']}</td><td>{$row['Detail']}</td>";
I'm trying to create something like the following:
But my current code has given me:
The part in white is what I get with the code below, and the part in black is what I get when I add in two more <td> rows. The problem is I can't get 3 pieces of data per row to be created - only vertically or horizontally.
$result = mysqli_query($con,"SELECT * FROM show_listing");
while($row = mysqli_fetch_array($result))
{
echo "<table>
<tr>
<td>".$row['name']."'</td>
</tr>
</table>";
}
mysqli_close($con);
It may be simpler than I think but does anyone know how I could do this?
$c = 0;
echo "<table>";
while($row = mysqli_fetch_array($result))
{
if ($c%3 == 0) {
if ($c) echo "</tr>";
echo "<tr>";
}
echo "<td>".$row['name']."</td>";
$c++;
}
if ($c) echo "</tr>";
echo "</table>";
You have the correct idea. You do not want the declaration in the while loop as the result is that you will get multiple tables.
So I moved the table declarations outside of the while loop. I also made the declarations conditional based on the current column. You can now set the variable $columns to however many columns you want.
$result = mysqli_query($con,"SELECT * FROM show_listing");
echo "<table>"
$columns=3;
$current_column=1;
while($row = mysqli_fetch_array($result))
{
if($current_column==1) {
echo "<tr>";
}
echo "<td>".$row['name']."'</td>";
if($current_column==$columns) {
echo "</tr>";
$current_column=1;
} else
$current_column++;
}
echo "</table>
mysqli_close($con);
Back with another quick question. I have this code below which echo's out product names from a database. What I want to do is make the echoed out product names a link to another page called product.php, each link needs to have a unique ID, for example
Product Name
How would I go about doing this? Many thanks. I will point out that I am very new to PHP.
<?php
//create an ADO connection and open the database
$conn = new COM("ADODB.Connection");
$conn->open("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=C:\WebData\Northwind.mdb");
//execute an SQL statement and return a recordset
$rs = $conn->execute("SELECT product_name FROM Products");
$num_columns = $rs->Fields->Count();
echo "<table border='1'>";
echo "<tr><th>Name</th></tr>";
while (!$rs->EOF) //looping through the recordset (until End Of File)
{
echo "<tr>";
for ($i=0; $i < $num_columns; $i++) {
echo "<td>" . $rs->Fields($i)->value . "</td>";
}
echo "</tr>";
$rs->MoveNext();
}
echo "</table>";
//close the recordset and the database connection
$rs->close();
$rs = null;
$conn->close();
$conn = null;
?>
Assuming your Products table has a unique ID field called "id", change your select to:
$rs = $conn->execute("SELECT id, product_name FROM Products");
And when you want to create a link, use that field and pass it into the URL. So you'd have product.php?id=<?= $thatIdField; ?>.
Example code:
echo "<table border='1'>";
echo "<tr><th>Name</th></tr>";
while (!$rs->EOF) //looping through the recordset (until End Of File)
{
echo "<tr>";
for ($i=0; $i < $num_columns; $i++) {
echo "<td>" . $rs->Fields($i)->value . "</td>";
}
echo "</tr>";
$rs->MoveNext();
}
echo "</table>";