PDA

View Full Version : using two tutorials, help


ali_b
04-04-2007, 04:37 PM
hi, id first like to say thank you for making these video tutorials, i feel like i am actually learning rather than just copying code from a written tutorial and thinking, how does this work :S

What i need help with is this, I watched your dynamic dropdown menu tutorial and the tutorial about mysql and php where it automatically made new rows in the table, both of these are great.

On my website i have a dropdown table filled will local towns and in a database i have shops in these towns.
when you click on a town, say, north berwick, i want it to reload the page and make a table will all of the shops in north berwick and if i clicked on drem, it would list all the shops in drem and so on...

Could you please help me on how to do this, if i get this sorted thats my website basically made, i have no problems with anything else at the moment (touch wood).

Thank you in advance,

Ali

davidj
04-04-2007, 05:34 PM
do you have the dropdowns working from the tutorials?

ali_b
04-04-2007, 05:41 PM
i havent actually done any of the tutorials i just watched them all 1st. i dont know what i should do so that you only display "north berwick", should i do a new table in the database for each town or should i do it so that the ID field isnt autoincrement so that i can put the id for all the shops in north berwick to 1 and all the shops in drem to 2. is that on the right track? i dont have time just now to do any coding but if that is on the right track i will do it when i have time.

davidj
04-04-2007, 05:59 PM
you need a town table and a shop table

the shop table should contain an id which relates to the town table id

example...
town table

blyth = 1
seaton deleval = 2
seaton sluice = 3
whitley bay = 4

shop table
shop1 = 1
shop2 = 1
shop3 = 1
shop4 =2
shop5 =2

in this example shops 1, 2, and 3 belong to town 1 and shops 4 and 5 belong to town 2

ali_b
04-04-2007, 06:01 PM
cheers, that makes sense, basically the same as in tutorial. Thank you veeeery much, hopefully ill get it to work ill post my code if it doesnt work when i get round to doing it

ali_b
04-05-2007, 12:15 PM
ive attempted some code but i need a little guidance now.

so far i have:

the php:

<?php
require_once("connections/connection.php"); //database connection

$town = $_POST['town'];
if($town) {
/////////////////////////////////////////////////////
$query = sprintf("SELECT * FROM shops where shop_id='$town'")
$result = @mysql_query($query);
$rowShop = mysql_fetch_array($result);
////////////////////////////////////////////////////
}
?>

and the form:

<form id="form1" name="form1" method="post" action="dropdown.php">
<select name="town" onchange="document.forms[0].submit()">
<option value="" selected>Select Your Town</option>
<option value="1">North Berwick</option>
<option value="2">Drem</option>
<option value="3">Gullane</option>
<option value="4">Aberlady</option>
</select>
</form>

there may be something wrong with the top line of the form because it is displayed in black.

on your tutorial you have

do {
echo $row['field2']."<br>";
}while ($row = mysql_fetch_array($result))

so i would do
do {
echo $rowShop['field2']."<br>";
}while ($rowShop = mysql_fetch_array($result))

I dont really know what to put where you put 'field2' because mine depends on what was submitted in the form. could you please help me out with this bit,

Cheers

davidj
04-05-2007, 05:10 PM
'field2' relates to the field in the database

ali_b
04-05-2007, 06:20 PM
im still kinda stuck, so where 'field2' is i would put 'shops' because that is the only field in the table called shops i want to display?

davidj
04-05-2007, 06:21 PM
yes

if shops is the field in the db then thats what you use

ali_b
04-05-2007, 06:25 PM
kl thanks, ill give it a go now

davidj
04-05-2007, 06:31 PM
mysql_fetch_array is a function whch returns an array of the db row

and as this is being set ...

$rowShop = mysql_fetch_array($result);

then that array is being set to the $rowShop variable. The array uses the database field names as keys so you can target any value by using the array method


$rowShop['db_field_name']//<< correct syntax

ali_b
04-05-2007, 08:58 PM
hmm, i have

<?php do { ?>
<tr>
<td><?php echo $rowShop ['shops']?></td>
</tr>
<?php } while ($rowShop = mysql_fetch_array($result)); ?> ////this is line 49
</table>

and i get the error :

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/menugenc/public_html/test/dropdown/dropdown.php on line 49
shop

so im obvioussly doing somming wrong but i dont know what.

davidj
04-05-2007, 09:12 PM
where you have this..

<?php } while ($rowShop = mysql_fetch_array($result)); ?>

do this (notice the @)


<?php } while ($rowShop = @mysql_fetch_array($result)); ?>


its just a warning which is displaying correctly

just use an @ to supress it

ali_b
04-05-2007, 09:16 PM
i got the same error on line nine, so i put another @ sign but now when i do it it just displays "shop" (because its in the first <td>)

do you want me to post all my code? or is it a problem with the databases?

davidj
04-05-2007, 09:19 PM
yes please

ali_b
04-05-2007, 09:19 PM
<?php
require_once("connections/connection.php"); //database connection

$town = $_POST['town'];
if($town) {
/////////////////////////////////////////////////////
$query = sprintf("SELECT * FROM shops where shop_id='$town'");
$result = @mysql_query($query);
$rowShop = @mysql_fetch_array($result);
////////////////////////////////////////////////////
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>

<form id="form1" name="form1" method="post" action="dropdown.php">
<select name="town" onchange="document.forms[0].submit()">
<option value="" selected>Select Your Town</option>
<option value="1">North Berwick</option>
<option value="2">Drem</option>
<option value="3">Gullane</option>
<option value="4">Aberlady</option>
</select>
</form>

<table>
<tr>
<td>Shop</td>
</tr>

<?php do { ?>
<tr>
<td><?php echo $rowShop ['shops']?></td>
</tr>
<?php } while ($rowShop = @mysql_fetch_array($result)); ?>
</table>

</body>
</html>

davidj
04-05-2007, 09:23 PM
where you have ...

<?php echo $rowShop ['shops']?>

do this


<?php echo $rowShop['shops'];?> //<< no space

ali_b
04-05-2007, 09:26 PM
nope still nothing :(

davidj
04-05-2007, 09:28 PM
try this...

wher you have


while ($rowShop = @mysql_fetch_array($result)); ?>


change to this


while ($row = @mysql_fetch_array($result)); ?>

ali_b
04-05-2007, 09:33 PM
no still nothing. could it be a problem with the database?

in the shops table it has a warning that says:

No index defined!

is this significant?

ali_b
04-05-2007, 09:33 PM
no still nothing. could it be a problem with the database?

in the shops table it has a warning that says:

No index defined!

is this significant?

davidj
04-05-2007, 09:39 PM
did you follow the tutorials about connecting to a database

ali_b
04-05-2007, 09:43 PM
yes, as in the connection.php?

i didnt know how you created those directories i.e. the root ones so i just uploaded it to my webspace.

davidj
04-05-2007, 09:46 PM
you need to understand the tutorials. You cant just copy them

davidj
04-05-2007, 09:47 PM
if you dont follow the tutorials to the letter then they wont work

ali_b
04-05-2007, 09:59 PM
ok, ive set the www directory to my documents but i dont know how to creat the web root and local root directories

davidj
04-05-2007, 10:03 PM
you need to set the web root as the www dir

create 2 directories in my documents (named local and remote)

set remote to www from the wamp5 installation