Deleting specific records after showing them - php

I am learning php and I am trying to learn how to show a sql table and be able to delete specific id's in a table.
In the code below, I am echoing a table called Project. My table is only showing Projects created by this user. All this works fine.
I would now like to let the user delete a specific project with a button shown next to each project.
<?php
if( isset($_COOKIE['user_id_cookie']) ){
$my_Query = "SELECT * FROM Project WHERE user_id=".$_COOKIE['user_id'];
$result = mysqli_query($con, $my_Query);
echo "<table id=\"table\"><thead class=\"thead\"><th>Project Name</th><th>Date Modified</th></thead>";
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
echo "<tbody><tr><td>".$row['Name_Project']."</td><td class=\"date\">".$row['Date_Project']."</td></tr></tbody>";
}
echo "</table>";
mysqli_close($con);
}
?>
I imagine it looking like this in the end:
||Project Name | Date Modified | ||
|| some name | some date | Delete ||
I would like Something looking like this
How would I best go about this?

Related

Get row values as column names using Codeigniter and mysql

im using codeigniter and mysql as db. i know might can be solved with mysql query, but if anyone can solve with codeigniter query it will be awesome, otherwise normal PHP mysql query can work also.
I have Table with 3 columns, Well it have many feilds but i have shown here 4 enteries, i didnt wanted to make a table with many columns but only 1 row of data. so instead i went for this style of table as someone suggested me on this website.
Problem is i never worked with this kind of table.
SettingsID SettingsKey SettingsValue
----------------------------------------------------------------------
1 | facebookLink | facebook.com
2 | twitterLink | twitter.com
3 | youtubeLink | youtube.com
4 | googlePlusLink | googleplus.com
Now i want to run a query that should return me rows in to columns. i searched over net and found some solutions but i am not good with this new queries.
I suppose this guy had same kind of problem like i have but in his case he has same value repeated in column, where my SettingsKey has all the values unique.
Here is the link to similar kind of question :
mysql select dynamic row values as column names, another column as value
i cant understand his query and that query i am not sure if is any use of me.
Please can anyone help me build a query to return row values of SettingsKey as columns.
This should work:
<?php
$db = new PDO('mysql:host=localhost;dbname=DBNAME;charset=utf8', USERNAME, PASSWORD);
$stmt = $db->query("SELECT SettingsKey, SettingsValue FROM Settings");
$links = array();
while($row = $stmt->fetch()) {
$links[$row['SettingsKey']] = $row['SettingsValue'];
}
$db = null;
This code queries this table and for each row the SettingsKey and the SettingsValue will be shown.
Now you can get the facebookLink like this:
echo $links['facebookLink'];
Hope this helps.
$sql = "
SELECT SettingsKey, SettingsValue
FROM Settings
";
$q = $this->db->query($sql);
return $q->result_array();
Go to your controller
$data['settings'] = $this->model_selection->your_function();
$this->load->view('example', $data);
And lasty on your view
<?php if(!empty($settings)): ?>
<?php foreach($settings as $k => $v): ?>
//print your staff in here mix with html and go crazy
<?php endforeach; ?>
<?php endif ?>

Creating links with tables names that show table information when clicked

I'm trying to create a page that shows the name of multiple mysql tables and creates a link for each one. When a user clicks a link, he's taken to a page that shows him the table contents.
For example:
Link with table name.
Table name
The user clicks the link and is taken to the page contents.php. That page prints the table contents.
For example if I have a table with Name and Age columns and John and 24 inserted in the columns, the page would print John and 24.
I appreciate if anybody can help me.
It would be way easier to do this with a GET request rather than a post request.
Table name
Then in your contents.php file:
<?php
$tablename = $_GET["tableName"]
/* all your queries that you want to do with tablename goes here */
?>
try this one...
$result = mysql_query("show tables"); // run the query and assign the result to $result
while($table = mysql_fetch_array($result)) { // go through each row that was returned in $result
echo "<a href=\"content.php?tableName=".$table[0]."\" >".$table[0] . "</a><BR>"; // print the table that was returned on that row.
}
content.php...
if(isset($_GET["tableName"]))
{
$select="select *from ".$_GET["tableName"];
$result=mysql_query($select);
while($row=mysql_fetch_array($result))
{
echo $row["name"];
echo $row["age"];
}
}

How do I generate a unique URL for each row in a table?

I'm still new to PHP and MySQL. I'm currently working on a random quote generator website. When a user visits for the first time or refreshes the page, the PHP code fetches a random row from the MySQL table and echos the results.
If a user likes a particular quote, I want him/her to be able to bookmark the page the quote is contained in. I believe this requires a unique URL for each random code that is generated. I can't figure out how to do this with the code I currently have and would like anyone's help.
This is my table so far:
+----+-----------+-----------+
| id | quote | source |
+----+-----------+-----------+
| 1 | hello | test1 |
| 2 | world | test2 |
| 3 | random | test3 |
+----+-----------+-----------+
This is my code so far:
<?php
require('connection.php');
// Last query result stored in sessions superglobal to avoid immediately repeating a random quote
session_start();
if (empty($_SESSION['lastresult'])) {
$_SESSION['lastresult'] = null;
}
$query = "SELECT * FROM `test` WHERE `id` != '%s' ORDER BY RAND() LIMIT 1";
$query = sprintf($query, mysql_real_escape_string($_SESSION['lastresult']));
$result = mysqli_query($dbc, $query) or die(mysql_error());
if (mysqli_num_rows($result) == 1) {
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$_SESSION['lastresult'] = $row['id'];
echo '<p>' . $row['quote'] . '</p>' . '<p>' . $row['source'] . '</p>';
}
} else {
echo '<p>Sorry, there was an error. Please try again by refreshing the page or clicking the query button.</p>';
}
?>
Any other code advice would also be appreciated.
There are three ways that spring to mind.
All require you to implement a version if the page that takes a quote id, similar to the one #DanGoodspeed suggests in his comment against your question.
Add a link from you existing page to the new id driven page with a label 'bookmark this page to return'. Not ideal, but very simple.
Add some JavaScript in your existing page to update the URL after loading to include the id, therefore making it appear as if it was the other. See this answer Modify the URL without reloading the page for details.
Instead of using the page you have now, generate a new page (using much of what you have already written) that returns a location redirect header with a URL that has a random id in it.

Display mysql in separate tables

Hi I am just a beginner with phpmysql and trying to display result in separate table by a row.
I have this query:
$albania = mysqli_query($con,"SELECT * FROM `data` ORDER BY citid ASC");
while($row = mysqli_fetch_array($albania))
I know that this will output all results onto 1 table. I want to put results onto separate tables dependent on the $row['citizenship']
I have about 40 different results in that row, so I want to separate it by that row and output separately.
If you order by the field you want to split on (citizenship) and keep track of the current value (done below with $currentCitizenship), then you should be able to just look for changes in that value and start a new table when the next section starts.
$albania = mysqli_query($con,"SELECT * FROM `data` ORDER BY citizenship ASC, citid ASC");
$currentCitizenship = ""
while($row = mysqli_fetch_array($albania)){
//handle the first time through the loop and start the first table
if ($currentCitizenship = ""){
//Start the first table
echo "<table>";
$currentCitizenship = $row['citizenship'];
}elseif ($currentCitizenship != $row['citizenship']){
//change to next citizenship table
echo "</table><table>";
$currentCitizenship = $row['citizenship'];
}
/*
Your current row printing code
*/
}
//close off the last table
echo "</table>"
What you wrote will not output anything yet. It will just fetch all rows from the database. You need to add code in PHP to diplay it in the screen.
Something like this. Need to put the correct php syntax:
$albania = mysqli_query($con,"SELECT * FROM `data` ORDER BY citid ASC");
while($row = mysqli_fetch_array($albania))
{
if($row['citizenship'] equals 'valuea')
print in one table
else
print in another table
}

How to I pull information from MySQL with PHP?

I am asking if it is not only possible to pull data from a MySQL table, but also display each row in either a table or a DIV, preferably a DIV. How would I do so?
Table:
ID |BodyText |Title |
1 |Hello World1 |Title1 |
2 |Hello World2 |Title2 |
etc..
I'd like to put each row into a DIV that has a title and also bodytext, but I want php to generate these tables and put the info into each DIV.
Possible to do?
We will use this table as an example data set:
Database Name: friends
-----------------------------
| firstname || lastname |
----------------------------
|Chris || Geizz |
|Steve || Michalson |
|Ken || Bohlin |
|Doug || Renard |
-----------------------------
First you must connect to your database as so:
http://www.w3schools.com/PHP/php_mysql_connect.asp
You would run a MYSQL query to get the data from the MySQL Database:
$query = mysql_query("SELECT * FROM friends");
Then you would use the following to put them into a table:
echo "<table><tr><td>First Name</td><td>Last Name</td></tr>";
while($row = mysql_fetch_array($query))
{
echo "<tr><td>";
echo $row['firstname'];
echo "</td><td>";
echo $row['lastname'];
echo "</td></tr>";
}
echo "</table>";
What this basically does is pulls the information that is returned with my mysql_query and puts it into an array that you can call with the $row variable we set. We do this in the while loop so that it repeats for all records in your database.
The code before the while loop and after the while loop are there so that way it is all in one table and we are just making rows instead of separate tables for each row in your database.
This will accomplish what you want. When you are doing the while loop you can use the variables ($row['firstname'] and $row['lastname']) as you wish. You could place them in seperate DIV's if you wish as well.
Hope this helps you! If you have any questions leave a comment and I will respond.
Basically you can do whatever you want, once you have the rowset. The general structure of such code (if layers not devided / MVC) looks like this:
connect to db
write query
retrieve results
loop for each row in resultset
{
echo html and column content as you wish
}
so for example you could use
foreach ($rowset as $row)
{
echo '<div>'.$row['column1'].' is a friend of '.$row['column2'].'</div>';
}
Here's the manual pages for how to access/query MySQL databases:
MySQL: http://www.php.net/manual/en/book.mysql.php
MySQLi: http://www.php.net/manual/en/book.mysqli.php
Using the functions there you can easily query and display the data as you see fit. If you are wanting to access column info (field names) you either need to use the access the information_schema tables or use queries like describe table.
Update: based on the comments, the question relates more to Tharkun's answer than this.
<?php
$result = mysql_query("SELECT * from table");
if($result && mysql_num_rows($result))
{
$numrows = mysql_num_rows($result);
$rowcount = 1;
while($row = mysql_fetch_assoc($result))
{
print "<div><b>$rowcount</b>";
foreach($row as $var => $val)
{
print"|$var: $val|";
}
print "</div>";
++$rowcount;
}
}
?>

Categories