Page 1 of 1
Queries in php
Posted: Mon Jan 16, 2012 8:40 am
by markyboy17
Ok
What i have is a button on a page called invoices. now when this button is clicked it goes to the invoices page.
Now on this page i want it to display a table showing the top 300 or so records returned from a basic select query.
My question is how do i get it to display the rows returned by the query in such a table and then a possible drop down to expand the query.
Also if possible once it displays the results i would like the record to be editable somehow so they could update certain fields.
Any help would be greatly appreciated im really struggling
Thanks in advance.
Re: Queries in php
Posted: Mon Jan 16, 2012 9:03 am
by social_experiment
markyboy17 wrote:My question is how do i get it to display the rows returned by the query in such a table and then a possible drop down to expand the query.
You don't paste any code so i'll work on the assumption that you have none at the moment.
To display data once you have selected it is passed to a function that fetches the data; mysql_fetch_array, mysql_fetch_row or mysql_fetch_object.
Code: Select all
<?php
$query = "SELECT field, field1 FROM table LIMIT 0, 300";
$sql = mysql_query($query);
// start to display data
while ($ary = mysql_fetch_array($sql)) {
echo $ary['field'];
echo $ary['field1']
}
This will display all the records, starting from record 0 until record 300; Here is a url where you can read some more on the syntax
http://www.plus2net.com/sql_tutorial/sql_limit.php
To have this option as a dropdown menu you need to modify your query to accept input from a form;
Code: Select all
<?php
if (isset($_POST['dropdownValue'])) {
$limit = $_POST['dropdownValue'];
}
else {
$limit = 300;
}
// select query will look like this
$query = "SELECT field, field1 FROM table LIMIT 0, ' . $limit . '";
$sql = mysql_query($query);
// proceed as normal
To have editable fields you would have to place your data (each row) inside a form or place forms where you want to edit.
Hth
Re: Queries in php
Posted: Mon Jan 16, 2012 12:19 pm
by califdon
markyboy17 wrote:Ok
What i have is a button on a page called invoices. now when this button is clicked it goes to the invoices page.
Now on this page i want it to display a table showing the top 300 or so records returned from a basic select query.
My question is how do i get it to display the rows returned by the query in such a table and then a possible drop down to expand the query.
Also if possible once it displays the results i would like the record to be editable somehow so they could update certain fields.
Any help would be greatly appreciated im really struggling
Thanks in advance.
That's a very broad question. You are essentially asking, "
How do I write a PHP script to display database records that can be edited by the user, updating the database, and flexibly displayed in pages?" This is much, much more than can be answered in a couple of posts to a forum! I think the best way to help you is to refer you to several online resources that deal with these programming techniques.
For basic display of database data in an HTML table, I recommend:
http://scriptplayground.com/tutorials/p ... -with-PHP/
For pagination (displaying a particular number of records at a time and allowing the user to page forward and back), I recommend:
http://www.phpfreaks.com/tutorial/basic-pagination
For making the data editable (and updating the database), all the above will have to be done with the data elements displayed in an HTML Form, then another PHP script (or the same one, if you divide your script into 2 sections and determine each time whether it is being called upon to display the data or update the database) will update the database. For this, I recommend:
http://blog.chapagain.com.np/very-simpl ... ay-in-php/