Forum Moderators: coopster

Message Too Old, No Replies

Email Results to Mysql > then view

         

someguyinaussie

2:41 pm on Nov 15, 2003 (gmt 0)

10+ Year Member



Hey people,

I have been working with Flash and PHP for a lil while but I've got a bigger project of my own to do.

Basically, I want to have a form, which will submit data to mysql, then I would like to be able to go online to a specific page and view the results in a table.

Like -

date ----- ¦---- name ----- ¦ ------ email -----------¦
10.12.03 ------- Mrs.Warm ---- marm@warmer.com -----
11.12.03 ------- mr.duck ------ duck@duckover.com ----

Results like that then you be able to click on one of the rows and show all the results submitted from the user, not just date, name, email...everything from the form they submitted, in a decent viewable layout so it can be printable.

I hope people can help and guide me along this journey ;)

Thank you ever so much

dreamcatcher

5:40 pm on Nov 15, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to Webmaster World, someguyinaussie!

What you should do is design a static HTML page and then work the MySQL and PHP code functionality around that.

Structure your results in a table, then just use a for loop to get the data from the database. Something like:

$query = "SELECT * FROM table";
$result = mysql_query($query);

<div align="center"><center>

<table border="0" width="80%">
<tr>
<td align="center" width="33%">Date</td>
<td align="center" width="34%">Name</td>
<td align="center" width="33%">E-Mail</td>
</tr>

<?

for ($i=0; $i<$row = mysql_fetch_array($result); $i++)

{

<tr>
<td>$row[date]</td>
<td>$row[name]</td>
<td>$row[email]</td>
</tr>

}

?>

</table>
</center></div>

I doubt anyone here will have time to design the whole thing for you as they probably won`t have time. No fun in learning if someone else is doing it for you anyway I don`t think.

Hope that helps in some way. :)

someguyinaussie

7:03 pm on Nov 15, 2003 (gmt 0)

10+ Year Member



Hey, that's exactly what I needed, I have the data put into the database...thanks...one more quicky -

The system Im making is basically storing information from a form to the database then I want to show all descriptions like above then that row of the table would link to the full data of the selected. Which will be set out readable and be bale to print.

How would I link, for each row to show the information? and how could I create a function so that I could print a selected amount of the data? Say I clicked on the first link, it would open a page with tha information in it, then I could print it, but what about if I wanted to print say all or even 10 selections at al time?

Thanks immensely!

dreamcatcher

10:25 pm on Nov 15, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How would I link, for each row to show the information?

When you store the info, its best to give each row a unique id number. ie:

id int(4) NOT NULL auto_increment,

Then when you pull all the data from the row, you`ll also pull that unique id number. So taking the original table I did, you would do:

$query = "SELECT * FROM table";
$result = mysql_query($query);

<div align="center"><center>

<table border="0" width="80%">
<tr>
<td align="center" width="25%">ID</td>
<td align="center" width="25%">Date</td>
<td align="center" width="25%">Name</td>
<td align="center" width="25%">E-Mail</td>
</tr>

<?

for ($i=0; $i<$row = mysql_fetch_array($result); $i++)

{

<tr>
<td>$row[id]</td>
<td>$row[date]</td>
<td>$row[name]</td>
<td>$row[email]</td>
</tr>

}

?>

</table>
</center></div>

To then get the info unique to that id, you could just add a url with a query string, for example:

<td><a href="print.php?id=$row[id]">$row[id]</a></td>

Then on your print page, you would do something like:

if (isset($id))

{

$query = "SELECT * FROM table WHERE id = '$id'";
$result = mysql_query($query);

...here again display the info for that link as above

}

Say I clicked on the first link, it would open a page with tha information in it, then I could print it, but what about if I wanted to print say all or even 10 selections at al time?

If you want to select a certain amount of entries, you would use the LIMIT clause.

Firstly create a function called say printALL(); This displays a specified amount of entries when its loaded.

Inside this function you would use the limit clause:

$query = "SELECT * FROM table LIMIT 0,10";
$result = mysql_query($query);

Change the limit to whatever you want. You can also order it by something. ie:

$query = "SELECT * FROM table ORDER by id LIMIT 0,10";
$result = mysql_query($query);

This would print the first 10 entries ordered by id.

On your page with the database info, you would do a URL again with a query string, something like:

print.php?action=printall

Then on your print page, load the function:

if ($_GET['action'] =="printall) { printAll(); }

Is this making any sense? I should be doing something more with my Saturday nights. LOL!

When you create your print page, make sure its just black and white, ie, printer friendly.

Hope that helps!

:)

[edited by: dreamcatcher at 10:39 pm (utc) on Nov. 15, 2003]

coopster

10:29 pm on Nov 15, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I should be doing something more with my Saturday nights. LOL!

LOL! Me too! But I do have plans, and you seem to have this covered quite well dreamcatcher. Have a good one -- coopster
<edit>
dreamcatcher has more time on this Saturday night to provide more direction. See his post instead ;)
<edit>

>>How would I link, for each row to show the information?
Basically, as you are looping through the data to display on this first screen, you simply add your links. Let's say you wanted the link to be on the name. Then in the line of the for loop specifying the name, add your <a href> to the variable and direct it to your next page. That page would be a PHP receiving page that would grab the variables from the GET query string and once again access the database to retrieve the information you wish to format and display.
>> and how could I create a function so that I could print a selected amount of the data? Say I clicked on the first link, it would open a page with tha information in it, then I could print it, but what about if I wanted to print say all or even 10 selections at al time?
Do you mean ten items from the list on your first page? Or ten rows of information regarding the clicked link on that first page? Either way, you would simply change the SQL SELECT statement to retrieve the result set of information you want to loop through to format and display.

dreamcatcher

10:41 pm on Nov 15, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



But I do have plans, and you seem to have this covered quite well dreamcatcher. Have a good one -- coopster

Thanks coopster. Well, it was most of you guys who taught me stuff in the first place, so I`m repaying the compliment and helping others if I can.

:)

someguyinaussie

11:02 pm on Nov 15, 2003 (gmt 0)

10+ Year Member



Hey, I've got this dump -

DROP TABLE IF EXISTS mortgage;
CREATE TABLE mortgage (
ID int(4) NOT NULL auto_increment,
FirstName mediumtext NOT NULL,
SurName mediumtext NOT NULL,
Email mediumtext NOT NULL,
Telephone mediumtext NOT NULL,
Mobile mediumtext NOT NULL,
Address mediumtext NOT NULL,
Town mediumtext NOT NULL,
PostCode mediumtext NOT NULL,
EmploymentStatus mediumtext NOT NULL,
Occupation mediumtext NOT NULL,
ApplicantOneIncome mediumtext NOT NULL,
ApplicantTwoIncome mediumtext NOT NULL,
CurrentHome mediumtext NOT NULL,
PurchasePrice mediumtext NOT NULL,
Deposit mediumtext NOT NULL,
AmountToBorrow mediumtext NOT NULL,
CreditProblems mediumtext NOT NULL,
AdditionalComments mediumtext NOT NULL
) TYPE=MyISAM;

error
MySQL said:
Incorrect table definition; There can only be one auto column and it must be defined as a key

What is a key?!

someguyinaussie

11:29 pm on Nov 15, 2003 (gmt 0)

10+ Year Member



Ahh ok, I got that problem sorted, I put the ID as index, I still get all ID = 0, why is it not putting each in numeric ID? 1 2 3 4 etc Just get ID = 0 for like 5 items!

Do I have to set it in the form? Sorry about this guys but just bare with me please :)

ooops, EDIT -

I have it sorted now heh!

sorry about that, I'll crack on with the rest you gave me dreamcatcher!

Thank you friend!

Watch this space lol

someguyinaussie

11:50 pm on Nov 15, 2003 (gmt 0)

10+ Year Member



I give up, I just get errors from that table script you posted...

I've got -


<html>
<head>
<title>View data</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
$db = mysql_connect("localhost","dbname","pass");
mysql_select_db("db",$db);
$query = "SELECT * FROM mortgage";
$result = mysql_query($query);

<div align="center"><center>

<table border="0" width="80%">
<tr>
<td align="center" width="25%">ID</td>
<td align="center" width="25%">Date</td>
<td align="center" width="25%">Name</td>
<td align="center" width="25%">E-Mail</td>
</tr>

<?

for ($i=0; $i<$row = mysql_fetch_array($result); $i++)

{

<tr>
<td>$row[id]</td>
<td>$row[FirstName]</td>
<td>$row[SurName]</td>
<td>$row[Email]</td>
</tr>

}

?>

</table>
</center></div>
</body>
</html>

Just not working, get errors, when I edit too, Only slight one line error but still, not working!

Thanks in adnvaced...

dreamcatcher

1:02 am on Nov 16, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No problem. We are here to help. :)

Firstly, from the code you posted you are missing a closing PHP tag after:

$result = mysql_query($query);

after that line add:

?>

Also, if you get database problems, it might be best to change the above line to:

$result = mysql_query($query) or die(mysql_error());

This is useful for debugging purposes.

someguyinaussie

1:15 am on Nov 16, 2003 (gmt 0)

10+ Year Member



Thanks again lol

Ok, I did that but I also did that before but still I get problems with line error 29

which is the start of


line29 - <tr>
<td>$row[id]</td>
<td>$row[FirstName]</td>
<td>$row[SurName]</td>
<td>$row[Email]</td>
</tr>

Really depressing me now lol!

dreamcatcher

1:37 am on Nov 16, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, its because you don`t have opening and closing PHP tags inside the for loop. This will result in a parse error. Sorry I wasn`t a little clearer!

Change:

<tr>
<td>$row[id]</td>
<td>$row[FirstName]</td>
<td>$row[SurName]</td>
<td>$row[Email]</td>
</tr>

to:

echo "<tr>\n";
echo "<td>$row[id]</td>\n";
echo "<td>$row[FirstName]</td>\n";
echo "<td>$row[SurName]</td>\n";
echo "<td>$row[Email]</td>\n";
echo "</tr>\n";

:)

MWpro

4:11 am on Nov 16, 2003 (gmt 0)

10+ Year Member



Personally, I like to use the heredoc syntax for something like that; saves all the echo's.


echo <<<END
<tr>
<td>$row[id]</td>
<td>$row[FirstName]</td>
<td>$row[SurName]</td>
<td>$row[Email]</td>
</tr>
END;

someguyinaussie

12:55 am on Nov 17, 2003 (gmt 0)

10+ Year Member



Hey :)

Sorry to be like this but Ive got problems!

The print page code you gave me, keeps getting parse errors

[code]
</head>
<body>
<?php
$db = mysql_connect("localhost","","");
mysql_select_db("o",$db);
?>

<table border="0" width="80%">
<tr>
<td align="left" width="25%">ID</td>
<td align="left" width="25%">Date</td>
<td align="left" width="25%">Name</td>
<td align="left" width="25%">E-Mail</td>
</tr>
<?
if (isset($id))
{
$query = "SELECT * FROM mortgage WHERE ID = '$id'";
$result = mysql_query($query);
for ($i=0; $i<$row = mysql_fetch_array($result); $i++)
{
echo "<tr>\n";
echo "<td>$row[id]</td>\n";
echo "<td>$row[FirstName]</td>\n";
echo "<td>$row[SurName]</td>\n";
echo "<td>$row[Email]</td>\n";
echo "</tr>\n";
}
?>
</table>
</center></div>
</body>
</html>
[code]

Also the link in the$row, seems to get parse error too

Really need a helping hand

Thanks so much!

jatar_k

1:08 am on Nov 17, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



welcome to WebmasterWorld someguyinaussie,

since you didn't say what type of parse error or what line it's on it doesn't help much but I'll do part of your homework

change this

for ($i=0; $i<$row = mysql_fetch_array($result); $i++)
{
echo "<tr>\n";
echo "<td>$row[id]</td>\n";
echo "<td>$row[FirstName]</td>\n";
echo "<td>$row[SurName]</td>\n";
echo "<td>$row[Email]</td>\n";
echo "</tr>\n";
}

to

while ($row = mysql_fetch_array($result)){
echo "<tr>\n<td>" . $row[id] . "</td>\n<td>" . $row[FirstName] . "</td>\n<td>" . $row[SurName] . "</td>\n<td>" . $row[Email] . "</td>\n</tr>\n";
}

see if that works

someguyinaussie

1:37 pm on Nov 17, 2003 (gmt 0)

10+ Year Member



Ok, sorry about not specifiying the parse problem, I have changed it to you code and I still get parse error on 33


<html>
<head>
<title>View Data</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
$db = mysql_connect("localhost","o","");
mysql_select_db("o",$db);
?>

<table border="0" width="80%">
<tr>
<td align="left" width="25%">ID</td>
<td align="left" width="25%">Date</td>
<td align="left" width="25%">Name</td>
<td align="left" width="25%">E-Mail</td>
</tr>
<?
if (isset($id))
{
$query = "SELECT * FROM mortgage WHERE ID = '$id'";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)){
echo "<tr>\n<td>" . $row[id] . "</td>\n<td>" . $row[FirstName] . "</td>\n<td>" . $row[SurName] . "</td>\n<td>" . $row[Email] . "</td>\n</tr>\n";
}
?>
</table>
</center></div>
</body>
ERROR 33 HERE </html>


Why the h--l is it on the html syntax!

plus the link of the $row, what dreamcatcher gave me, dont seem to work, I just cant get it working in the bit instead of rowID, when I replace it I get parse error, saying expected ;

Thanks so much guys, Im a big trouble!

coopster

1:41 pm on Nov 17, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I'm not sure if your PHP parser is configured to parse
<?
tags, but it's probably better to stay consistent anyway. Change the line right above your

if (isset($id))

from
<?
to
<?php

someguyinaussie

2:00 pm on Nov 17, 2003 (gmt 0)

10+ Year Member



No, still same parse error :(

jatar_k

2:18 pm on Nov 17, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you are missing a closing brace

}
}
?>
</table>
your if statement has no closing brace

someguyinaussie

2:32 pm on Nov 17, 2003 (gmt 0)

10+ Year Member



Wooo hooo, thanks, works fine now!

Just one more lol

You can shoot me after this!


<?
for ($i=0; $i<$row = mysql_fetch_array($result); $i++)
{
echo "<tr>\n";
echo "<td><a href="details.php?ID=$row[ID]">$row[ID]</a></td>\n";
echo "<td>$row[FirstName]</td>\n";
echo "<td>$row[SurName]</td>\n";
echo "<td>$row[Email]</td>\n";
echo "</tr>\n";
}
?>

What's wrong with that? says missing / expected " ; "

Thank you, I owe you all a beer!

coopster

5:13 pm on Nov 17, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You need to escape double quotes in a quoted string. Change this line to look like this:

echo "<td><a href=\"details.php?ID=$row[ID]\">$row[ID]</a></td>\n";

But, be aware that your for loop may not work as planned either. You are better off changing it to a while loop as jatar_k showed you earlier.

dreamcatcher

5:57 pm on Nov 17, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Glad to see things are falling into place someguyinaussie. :)

someguyinaussie

6:00 pm on Nov 17, 2003 (gmt 0)

10+ Year Member



I did use Jatar_K while loop, but got parse error, about " ; 2 that again!

I used -

<?
for ($i=0; $i<$row = mysql_fetch_array($result); $i++)
{
echo "<tr>\n<td>" <a href=\"details.php?ID=$row[ID]"\> . $row[ID] . </a> "</td>\n<td>" . $row[FirstName] . "</td>\n<td>" . $row[SurName] . "</td>\n<td>" . $row[Email] . "</td>\n</tr>\n";
}
?>

Dont work! :(

hey, and thanks for poping by DreamCatcher!

jatar_k

6:13 pm on Nov 17, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



because when you added the link to the echo statement you crossed up all the quotes

echo "<tr>\n<td><a href=\"details.php?ID=" . $row[ID] . "\">" . $row[ID] . "</a></td>\n<td>" . $row[FirstName] . "</td>\n<td>" . $row[SurName] . "</td>\n<td>" . $row[Email] . "</td>\n</tr>\n";

don't blame my while loop ;)

someguyinaussie

6:16 pm on Nov 17, 2003 (gmt 0)

10+ Year Member



I didnt blame your loop hehe Im just useless! ;)

Thank you guys, once again...first time ever on this site and I will use it again to be a trouble again!

Any you guys need Flash help and graphics, give me a call :)

someguyinaussie

11:45 am on Nov 21, 2003 (gmt 0)

10+ Year Member



Hey guys :)

Need help with the PrintAll() function, where do I start?

I want to be able to print off a selection of files from mysql, either 1 to 100, the user will chose whch ones he wants to print

thanks

Oh, whos watching the World cup final tomorrow?

coopster

1:10 pm on Nov 21, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



If you are referring to the PrintAll() function that dreamcatcher suggested earlier in this thread, it is a user defined function [php.net] that you will develop and call when needed. You're going to want to learn about user defined functions, you'll use them quite often in PHP ;)

dreamcatcher

2:55 pm on Nov 21, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, I was just using printAll() as an example. You really need to read up on functions like coopster says, they are one of the most useful things in PHP and you will use them a lot.

As for getting a specific amount of data from a database, you will be using a LIMIT claus.


$query = "SELECT FROM table WHERE blah = 'blah' LIMIT 1";
$result = mysql_query($query);

Depending on your query this would pull 1 entry from the database. So, if you wanted to have say a drop down menu with different values, ie:

<select name="total">
<option value="10">10</option>
<option value="20">>20</option>
<option value="50">>50</option>
<option value="100">>100</option>
</select>

you need to send the value of the above to the database for it to pull the specified amount of entries.

$total = $_POST['total'];
$query = "SELECT FROM table WHERE blah = 'blah' LIMIT $total";
$result = mysql_query($query);

Hope that helps.