So I made a test script for PHP output of a basic Mysql database. The problem is that I would like to make the auto increment called orderid to be able to become a link that I can click to open up a page that shows more info about that specific ID of that order.
It really seemed hard to find anything online and I have seen databases that have done this.
Also (not in this script) I have used CURRENT_TIMESTAMP but it doesn't put the current time, but does use the current date. What is the best way to get the current time (AM PM or 24 hour then convert it later) for MYSQL or PHP and input it?
Thank you for your help.
<?php
$con = mysqli_connect('localhost','user','password','database');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
else{
$sql="SELECT * FROM custinfo WHERE orderopen = 1";
$result = mysqli_query($con,$sql);
echo "<table border='1' bordercolor='blue' bgcolor='CCFFFF' cellpadding='0'>
<tr><h2> Open Orders:</h2>
<th>OrderID</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Phone</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td><h3><font color='blue'>" . $row['orderid'] . "</font></h3></td>";
echo "<td><h3><font color='blue'>" . $row['firstname1st'] . "</font></h3></td>";
echo "<td><h3><font color='blue'>" . $row['lastname1st'] . "</font></h3></font></td>";
echo "<td><h3><font color='red'>" . $row['phone'] . "</font></h3></td>";
echo "</tr>";
}
echo "</table>";
}
mysqli_close($con);
?>
Well, if you just want to add an a element to this line:
echo "<td><h3><font color='blue'>" . $row['orderid'] . "</font></h3></td>";
Then just add an a element to it:
echo "<td><h3><font color='blue'><a href='somePage.php?id=" . $row['orderid'] . "'>" . $row['orderid'] . "</a></font></h3></td>";
Being a link doesn't make it particularly special, it's still just HTML like any other HTML that you're already outputting.
Related
I have the below table in my site...
and what I want is that when i click on the read more link to redirect me to the page view_announcement.php and in this page to display me the whole data for this specific row.
For example, if we click on the link in the second row I want in the view_announcement.php to load all the data for this specific row.
My code in order to display this table is this...
<?php
$q = ($_GET['q']);
$con = mysqli_connect('localhost','root','smogi','project');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"project");
$sql="SELECT author,category,subject,content FROM announcements WHERE category = '".$q."'";
$result = mysqli_query($con,$sql);
echo "<table>
<tr>
<th>Author</th>
<th>Category</th>
<th>Subject</th>
<th>Content</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['author'] . "</td>";
echo "<td>" . $row['category'] . "</td>";
echo "<td>" . $row['subject'] . "</td>";
echo "<td>" . 'Read More' . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
The view_announcement.php file doesn't contain any code yet because i dont know what to write.
One way to do it is to append a query variable to the "Read More" links. You'll probably need a unique identifier, such as an ID number, on your announements table. If you don't have one yet, I suggest adding one and setting it up to auto-increment.
You would want to modify your query to include the unique ID number:
$sql="SELECT id,author,category,subject,content FROM announcements WHERE category = '".$q."'";
Then you would modify the loop which prints your table out to include those unique IDs in the URL to view_announcement.php
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['author'] . "</td>";
echo "<td>" . $row['category'] . "</td>";
echo "<td>" . $row['subject'] . "</td>";
echo "<td>" . 'Read More' . "</td>";
echo "</tr>";
}
And in your file view_announcement.php, you would make another SQL query to get the full data for a specific row, like this:
$sql="SELECT * FROM announcements WHERE ID = '".$_GET['id']."'";
If you click any button, that redirects to view_announcement.php file, where you can get the subject values.
Use that subject values in your query to get all the details which relates to that subject.
i am new somehow to PHP and mysql,
I've simple database that receives the values from php form as id, date, time, max, today, tomorrow... i can insert into the table, all ok, i can also fetch the data to a php page using the below code, all ok, i can see the table of the values inserted in the db, all ok...
my need is to crate a simple line or bar chart that shows the max (which is static number each day) and the today number will be the variable that will be defined by the chart, so if it is a line it will show better that each day where is the number from the max... forget about tomorrow, it is not important...
here is my code of the display of the table of the page...
i want the simplest way to complete this, imagine only i have one column (today) that is changed each while or each day, so i want to show it in a graph against the max number... so i think the line chart will be suite best...
<?php
$con=mysqli_connect("localhost","root","","test");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT id, date, time, max, today, tomorrow, FROM testtable1");
echo "<table border='1'>
<tr>
<th>id</th>
<th>date</th>
<th>time</th>
<th>max</th>
<th>today</th>
<th>tomorrow</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['time'] . "</td>";
echo "<td>" . $row['max'] . "</td>";
echo "<td>" . $row['today'] . "</td>";
echo "<td>" . $row['tomorrow'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
I recommend using something like Google Visualizations for line charts. Visualizations are easy to implement and you can write them directly on the page see their example Line charts
Alright so I am trying to basically take what's in $row['cardid'] and set it into the value of my div tag. Then on the html file this is echoing to, it would run my function which uses that value from the div tag. Right now when I use the onclick, it pulls up but says my value is undefined. So my question is why is my variable undefined when I pull it up using html?
<?php
$q=$_GET["q"];
$con = mysql_connect("*", "*", "*");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("*", $con);
$sql="SELECT cardset, cardname, cardnumber, cardid FROM cards WHERE cardname LIKE '%".$q."%' OR cardset LIKE '%".$q."%'";
$result = mysql_query($sql) or die ('Error: '.mysql_error ());
echo "<table border='1'>
<tr>
<th>#</th>
<th>Cardname</th>
<th>Card Set</th>
</tr>";
while ($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['cardnumber'] . "</td>";
// This is the troubled line.
echo '<td><div value="'.$row['cardid'].'" onclick="changeimage(this.value)">' . $row['cardname'] . '</div></td>';
echo "<td>" . $row['cardid'] . "</td>";
echo "<td>" . $row['cardset'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
I don't fully understand your question, but you mean just something like this?
echo '<td><div onclick="changeimage(this.id)" id="'.$row['cardid'].'">' . $row['cardname'] . '</div></td>';
Use ID instead of value basically..
Example: jsFiddle
div elements doesn't have a value property, but you can get the value of the attribute named value - you should use .getAttribute("value"):
echo '<td><div value="'.$row['cardid'].'" onclick="changeimage(this.getAttribute(\"value\")">' . $row['cardname'] . '</div></td>';
An example: http://jsfiddle.net/vaSYh/
Also, if you need to store data in DOM elements, use data- attributes. (Or just name them that way even if you don't have to support HTML5 right now; It will make the transition easier)
Don’t use this.value.
It’s not a form.
Use this.innerText or this.innerHtml to get the content of the div
If I understand correctly your answer, you want to alert the value of $row['cardid']?
With your code, you can't get the correct value, because you're mixing up javascript code with php code.
The 'this' keyword in your 'onclick' event handler, is the javascript function itself, then it has not a 'value' property.
Try this code, to alert the right value:
echo '<td><div value="'.$row['cardid'].'" onclick="changeimage('.$row['cardid'].'">'. $row['cardname'] . '</div></td>';
Basicaly having issues setting up a webpage which will taken in a student key entered by the user. This will then parse the student key to another file which will run it against a mysql backend to see what records this student already has. But can not get it working for the life of me please help I'm still a newb at this.
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("support_log", $con);
$result= mysql_query("SELECT student.first_name, student.surname, student.year_group, student.STKEY, student_log.issue
FROM `student` JOIN `student_log`
WHERE student.STKEY like '$_POST[stkey]'");
$result2 = mysql_query($result) or die("Error: " . mysql_error());
if(mysql_num_rows($result2) == 0){
echo("no records found");
} ELSE {
echo "<table border='1'>
<tr>
<th>First name</th>
<th>Surname</th>
<th>Year Group</th>
<th>Student Key</th>
<th>Issue</th>
</tr>";
while($row = mysql_fetch_array($result2))
{
echo "<tr>";
echo "<td>" . $row['First_Name'] . "</td>";
echo "<td>" . $row['surname'] . "</td>";
echo "<td>" . $row['year_group'] . "</td>";
echo "<td>" . $row['stkey'] . "</td>";
echo "<td>" . $row['issue'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
mysql_close($con);
?>
After changing my where statement to:
WHERE student.STKEY like '$_POST[stkey]'");
I am no longer reciving errors from PHP but now recieving the error Query was empty which is part of my code to detect if there is no results. Though I have tested that query in phpmyadmin and it spits out results. From looking at the code does anyone have any solutions? I have also checked the parse by running an echo on the post command to ensure the data being entered was correct.
Edit: Got rid of the whole result2 check now throwing a:
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\stkey_submit.php on line 24
Try $_POST['stkey'] instead of $_POST[stkey]
EDIT : if you use it in a query, it would be preferable to do :
$stkey = mysql_real_escape_string($_POST['stkey']);
$sql = "SELECT ....... like '$stkey'";
mysql_query($sql);
$result= mysql_query("SELECT student.first_name, student.surname, student.year_group, student.STKEY, student_log.issue
FROM `student` JOIN `student_log`
WHERE student.STKEY like " . $_POST["stkey"]);
How about storing the value of stkey on a variable before including it on the query?
$stkey = $_POST['stkey'];
$result= mysql_query("SELECT student.first_name, student.surname,
student.year_group, student.STKEY, student_log.issue
FROM `student` JOIN `student_log`
WHERE student.STKEY LIKE '%$stkey%'");
You might also want to use MySqli or PDO instead of the MySql database API. Take a look at this post from Nettuts: http://net.tutsplus.com/tutorials/php/php-database-access-are-you-doing-it-correctly/
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("dbsql", $con);
$result = mysql_query("SELECT * FROM testimonial where approved='Yes'");
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Review</th>
<th>Date</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['full_name'] . "</td>";
echo "<td>" . $row['review'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
If you are looking for that specific block of data to loop 10 times every time the page is loaded simply use a for() loop
for($i=0;$i<10;$i++)
{
// block of data
}
But I assume that that is not what you are asking since it would be impractical (as far as I can see).
To print 10 results, add
limit 10
to the end of your query. If you're using pagination, however, you will need to start the limit somewhere (e.g. limit STARTING_NUMBER, NUM_OF_RESULTS)
Good luck!
Dennis M.
replace
SELECT * FROM testimonial where approved='Yes'
with
$offset = 0; //calculate your offset here as per page;
SELECT * FROM testimonial where approved='Yes' limit $offset, 10