28, May 2012

Show MySQL query with button - webmaster forum

 
Webdigity webmaster forums
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: Show MySQL query with button
« previous next »
Pages: [1] 2 Print
Instabuck - The easy way to sell digital products online

Author Topic: Show MySQL query with button  (Read 2842 times)
OMG!I am geek
**
Posts: 55
366 credits
Members referred : 0


« on: Aug 24, 2006, 10:35:46 am »

Hi All

I have several tables in MySQL that I would like to generate a query view of those records, one table at a time, within a formatted HTML table on the same page, but I would want this to happen when the end user clicks on a button. I would also like a "Clear" button to clear that query from the screen.

Any suggestions or examples?

Thanks
gman
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #1 on: Aug 24, 2006, 10:39:52 am »

You mean you want to see the data stored in the table, like phpMyAdmin does?

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
OMG!I am geek
**
Posts: 55
366 credits
Members referred : 0


« Reply #2 on: Aug 24, 2006, 10:48:16 am »

Yes, but through a query that can be seen on the same page, and than a simple clear function to make the query disappear. Can this be done...I know the query part can be but not sure how to invoke it using a "Sumbit" like button

Thanks Nikolas

gman
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #3 on: Aug 24, 2006, 10:49:53 am »

You can use a <select> form and when you have a value to the post variable do the query that it sais

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
OMG!I am geek
**
Posts: 55
366 credits
Members referred : 0


« Reply #4 on: Aug 24, 2006, 10:51:55 am »

Sorry Nikolas but I am still a newbie..any help with code? No rush on this....thanks again  Grin
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #5 on: Aug 24, 2006, 10:55:48 am »

I will try to give you a guide...

Code:
<form method="post" action="http://pathToMyScript.php" />
<select name="table">
<option value="table1">Table1</option>
<option value="table2">Table2</option>
....
....
....
</select>
</form>

<?php
 
if ( !empty ($_POST['table'] )
 {
  switch ( 
$_POST['table'] ){
    case 
'table1':
       
$query "SELECT * FROM table";
       break;
    case 
'table2':
       
$query = .......
       break;
  }
 }
?>


Hope that helps Smiley

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
OMG!I am geek
**
Posts: 55
366 credits
Members referred : 0


« Reply #6 on: Aug 24, 2006, 11:21:40 am »

Thanks Nikolas, I will give that a shot...I appreciate it

OMG!I am geek
**
Posts: 55
366 credits
Members referred : 0


« Reply #7 on: Aug 24, 2006, 01:50:09 pm »

Alright...I got the code to work that queries the table data and formats it into a HTML table....came out alright...and I also setup the button to call that PHP code.

The issue is that the query is coming up in a separate window and I would like the data to appear below the button, on the same page.

I was looking at named anchors and not sure if that is the correct approach

Any thoughts?

Thanks
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #8 on: Aug 24, 2006, 02:07:08 pm »

You can do it in the same page, by using the same page for the form and the php code that parses the queries.

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
OMG!I am geek
**
Posts: 55
366 credits
Members referred : 0


« Reply #9 on: Aug 24, 2006, 02:21:08 pm »

Thanks Nikolas...just not sure how to do that. When I load the query php page, it automatically shows the data...I only want it to show when I click on the button

Here is the code that I used to show the data in the table...it is called from another page, but like I said, I would like the button on the page as well and this data to appear below the button

show_data.php
Code:
<?php

// This code will draw a table with the data retrieved from the query.
 
function writeResult($result){ 
    if(gettype($result) != "resource" || get_resource_type($result) != "mysql result"){ 
        echo "Could not draw the table, not a valid mysql result."
        return; 
    
    $HTMLCode "<table border=\"1\">"
    $HTMLCode .= "<tr>";
for($i 0$i mysql_num_fields($result); $i++){ 
        $HTMLCode .= "<th>" mysql_field_name($result$i) . "</th>"
    
    for($i 0$i mysql_num_rows($result); $i++){ 
        $HTMLCode .= "<tr>"
        for($k 0$k mysql_num_fields($result); $k++){ 
            $data mysql_result($result$i$k); 
            $data = (($data == "") ? "&nbsp;" $data); // avoid empty strings 
            $HTMLCode .= "<td>" $data "</td>"
        
        $HTMLCode .= "</tr>"
    
    $HTMLCode .= "</tr>"
    $HTMLCode .= "</table>"
    echo $HTMLCode

$result mysql_query("select * from users") or die (mysql_error());

// Use the function written above... 
writeResult($result);

?>


So what I would like is the only thing on the page is the button when loading the page. When they click the button, the query data in the table appears on the same page below the button....hope this helps

Thank you
gman
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #10 on: Aug 24, 2006, 02:28:24 pm »

Can you post the form code also?

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
OMG!I am geek
**
Posts: 55
366 credits
Members referred : 0


« Reply #11 on: Aug 24, 2006, 02:43:22 pm »

So on one page I have this code

Code:
<form action="testDB1.php" method="post" name="form1" id="form1">
  <label>
  <input type="submit" name="Submit" value="Show Users" />
  </label>
</form>

So it loads the testDB1.php file that has the query info on it....I would like them all on one page
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #12 on: Aug 24, 2006, 02:51:53 pm »

Add to every page that you want it this :

Code:
<form action="<?=$_SERVER['PHP_SELF'];?>" method="post" name="form1" id="form1">
  <label>
  <input type="hidden" name="showT" value="1" />
  <input type="submit" name="Submit" value="Show Users" />
  </label>
</form>
<?php
include 'testDB1.php';
?>


And then change the testDB1.php to this :

Code:
<?php

// This code will draw a table with the data retrieved from the query.
 
function writeResult($result){ 
    if(
gettype($result) != "resource" || get_resource_type($result) != "mysql result"){ 
        echo 
"Could not draw the table, not a valid mysql result."
        return; 
    } 
    
$HTMLCode "<table border=\"1\">"
    
$HTMLCode .= "<tr>";
for(
$i 0$i mysql_num_fields($result); $i++){ 
        
$HTMLCode .= "<th>" mysql_field_name($result$i) . "</th>"
    } 
    for(
$i 0$i mysql_num_rows($result); $i++){ 
        
$HTMLCode .= "<tr>"
        for(
$k 0$k mysql_num_fields($result); $k++){ 
            
$data mysql_result($result$i$k); 
            
$data = (($data == "") ? "&nbsp;" $data); // avoid empty strings 
            
$HTMLCode .= "<td>" $data "</td>"
        } 
        
$HTMLCode .= "</tr>"
    } 
    
$HTMLCode .= "</tr>"
    
$HTMLCode .= "</table>"
    echo 
$HTMLCode

$result mysql_query("select * from users") or die (mysql_error());

// Use the function written above... 
if ( !empty($_POST['showT']) ) 
      
writeResult($result);
?>


I guess this will work, but to be honest I don't understand the use of it Smiley

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #13 on: Aug 24, 2006, 03:09:20 pm »

nice code Nick....

post it to the code gallery...

OMG!I am geek
**
Posts: 55
366 credits
Members referred : 0


« Reply #14 on: Aug 24, 2006, 03:14:26 pm »

I am getting a Fatal error stating that it cannot redeclare writerresult...will have to look into it further...thanks Nick for the updated code
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #15 on: Aug 24, 2006, 03:17:09 pm »

I am getting a Fatal error stating that it cannot redeclare writerresult...will have to look into it further...thanks Nick for the updated code

check that there is only one time

Code:
function writeResult($result){ ...

I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #16 on: Aug 24, 2006, 03:28:19 pm »

Change this :

include 'testDB1.php';

to this :

include_once 'testDB1.php';

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
OMG!I am geek
**
Posts: 55
366 credits
Members referred : 0


« Reply #17 on: Aug 24, 2006, 03:57:59 pm »

Thanks guys, all errors are gone but where should I put the DB Connect function? This is actually part of Olaf's Access Class and I have including the access_user_class.php file

It is stating I do not have access to the server.

Thanks again
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #18 on: Aug 24, 2006, 04:10:05 pm »

Thanks guys, all errors are gone but where should I put the DB Connect function? This is actually part of Olaf's Access Class and I have including the access_user_class.php file

It is stating I do not have access to the server.

Thanks again

is this file using the class?

OMG!I am geek
**
Posts: 55
366 credits
Members referred : 0


« Reply #19 on: Aug 24, 2006, 04:15:27 pm »

Well...I am trying to use the class...just not how to incorporate the code that Nikolas helped me out with and your code. Sorry for all of the questions...I appreciate the help
Trackback URI for this entry : http://www.webdigity.com/trackback.php?topic=3797
Tags : php html forums mysql php my admin Bookmark this thread : Digg Del.icio.us Dzone more....

Pages: [1] 2 Print 
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: Show MySQL query with button
« previous next »
Jump to:
User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 28, 2012, 03:20:29 pm





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!






Web Design Gallery · Whois Lookup · Pagerank · Tag Browsing · Lo-fi version · Syndication · Webmaster forum history · Advertise
Developed by HumanWorks © 2005 - 2012 Webdigity webmaster community · sublime directory
Webdigity Webmaster Forums | Powered by SMF 1.0.12. © 2001-2005, Lewis Media. All Rights Reserved.