Help - Search - Members - Calendar
Full Version: So i have an idea but dont know how to get it off the ground
Weborum Webmaster Forum > Web Page Design > PHP
jesse
Ok so heres the deal. I have a website which is campusgoal.com. In the lower left hand corner i have weather that is specific for my town of greeley. Problem is i want to expand this to other cities. So my idea is to get the code for diffrent cities and create a form on my site that when completed would remember that persons info and display their relevent weather. So for example here is the code for three cities i want to expand it to. Im thinking that using their zip code would be most effective for this. I have no i dea if this is possible but any help or info would be a god send. Thanks again for your time.
Jesse

CODE
<!--Weather image provided by www.areacodehelp.com--><a href="http://www.areacodehelp.com/cgi-bin/weather/weather.cgi?pands=fort collins, co" target="_blank"><img src="http://www.areacodehelp.com/cgi-bin/weather/weather.cgi?config=png&forecast=zone&alt=hwizone&pands=fort collins, co&hwvbg=FFFFFF&hwvtc=B5OCOO&hwvusettf=0" alt="Zone Forecast for Fort Collins, CO" border="0" /></a>

<!--Weather image provided by www.areacodehelp.com--><a href="http://www.areacodehelp.com/cgi-bin/weather/weather.cgi?pands=greeley, co" target="_blank"><img src="http://www.areacodehelp.com/cgi-bin/weather/weather.cgi?config=png&forecast=zone&alt=hwizone&pands=greeley, co&hwvbg=FFFFFF&hwvtc=B5OCOO&hwvusettf=0" alt="Zone Forecast for Greeley, CO" border="0" /></a>

<!--Weather image provided by www.areacodehelp.com--><a href="http://www.areacodehelp.com/cgi-bin/weather/weather.cgi?pands=boulder, co" target="_blank"><img src="http://www.areacodehelp.com/cgi-bin/weather/weather.cgi?config=png&forecast=zone&alt=hwizone&pands=boulder, co&hwvbg=FFFFFF&hwvtc=B5OCOO&hwvusettf=0" alt="Zone Forecast for Boulder, CO" border="0" /></a>
Waleed
I do not think this can be done with Javascript alone as you will need some sort of server-side language such as PHP or ASP to do the remembering part; perhaps with a cookie.
jesse
Yeah i had a feeling it would be pretty complex. Would you have any idea where i could start looking to do somthing like this. I barely get by with html but using php and cookies is beyond me.
Waleed
I have no experience with cookies, but you might find Joe's tutorial website handy (Joe is a mod here).

He has a few tuts on cookies that you might find helpful. Let us know if you need any help; if not me, someone else here will help you out. biggrin.gif
jesse
Thanks i'll give his tut a look over and see what i can do.
jesse
Ok so i have been messing around with those tutorials and i have something that looks close to what i would want to put on my site. But have no idea how to make it function. Also if you look at my site (www.campusgoal.com) I have a link to my school in the upper left hand corner. Along with the html for the weather in different cities i want to incorporate a form that when a person enters in their school name it would set a cookie that would bring up the graphic for their school as well as link it to their school website. Here is the php code for the form that looks like what i want to put on my website.
CODE
<?php
setcookie("weather");
setcookie("schoollink");
?>
<form method="post" name="cookie" action="process.php">

<p>
  <label for="username">Enter the name of your University Or College. <br>
  (ex:colorado state university)
: <input type="text" name="username" id="username" /></label></p>
<p>
  <label for="password">Enter ZIP Code for local weather :
  <input type="password" name="password" id="password" /></label></p>
<p><label for="setcookie"><input type="checkbox" name="setcookie" value="setcookie" " /> Remember My Homepage</label>
</p>

<p><input type="submit" name="submit" value="Submit" /> <input type="reset" name="reset" value="Reset" /></p>
</form>


I read the tutorials and the way i think this would work is that the username would be the school name. So for instance instead of creating a cookie for usernames and passwords i would have the usernames be the name of the schools i want to expand to. So when they type in the name of their school it would be that username with out a password. Along with this I would need to find a way to make the cookie call up their schools graphic with a <a href="http://www.myschoolswebpage.edu"/> link to it. I believe the same would be true with the html for the weather. The zip codes would be the usernames and when entered the cookie would bring up the appropriate html for their cities. This is by far the most complicated thing I have tried to do with my site so any help would be greatly appreciated.
Joe
Hi Jesse,

When storing values within cookies you would do it much the same way as you would with variables. If you take a look at;

http://us3.php.net/setcookie

you may get a better idea but I'll try to simplify it as much as possible for you.

From what I can see you want to store several sets of information about the user in cookies, so you'd be better off using a cookie array;

[php]<?php
// Set the variables.
$var1 = "cookie1";
$var2 = $_GET['input'];
$var3 = 45;

// Store the variables inside cookies.
setcookie("Cookie[One]", $var1);
setcookie("Cookie[Two]", $var2);
setcookie("Cookie[Three]", $var3);

// Output the variables.
echo $_COOKIE[Cookie]['One'];
echo $_COOKIE[Cookie]['Two'];
echo $_COOKIE[Cookie]['Three'];
?>[/php]

Basically, that's all you'd need to do with your cookies, set them and read them.
jesse
Hey Joe,
ok so ive been looking through the php site and trying to figure out how all of this works. What i cant seem to get the concept of is putting the php and the html together. do i attach the cookie to the html that i want it to use? somthing like this for example.

CODE
// Output the variables.
echo $_COOKIE[Cookie]['One'];
<!--Weather image provided by www.areacodehelp.com--><a href="http://www.areacodehelp.com/cgi-bin/weather/weather.cgi?pands=greeley, co" target="_blank"><img src="http://www.areacodehelp.com/cgi-bin/weather/weather.cgi?config=png&forecast=zone&alt=hwizone&pands=greeley, co&hwvbg=FFFFFF&hwvtc=B5OCOO&hwvusettf=0" alt="Zone Forecast for Greeley, CO" border="0" /></a>
echo $_COOKIE[Cookie]['Two'];
echo $_COOKIE[Cookie]['Three'];
?>[/php]


And how do would i incorporate this into the form? I actually just realized somthing when typing this. I only need the name of the persons school. Instead of needing them to enter their zip and school name i just need the name of the school and i would know what city that would be. So i guess what would be needed is a cookie that could do two things from one peice of information(the school name). when it says //set the variables why does $var1=cookie1 and $var2=$_GET['input']; and so fourth. Why dosent it go $var1=cookie1 $var2 = cookie2.
CODE
// Set the variables.
$var1 = "cookie1";
$var2 = $_GET['input'];
$var3 = 45;


So i go to school in colorado and i want to expand the site to the three biggest schools which are colorado university, university of colorado and my school university of northern colorado. Where in the php code would these be and how would the cookie impliment the html when one of the schools are entered? I appreciate you taking the time helping me with this.

P.S. I looked through your sites and i really enjoyed them. I like the feel, very clean and professional.
Joe
Jesse,

You cannot mix HTML and PHP together as PHP is a server side language and must be translated by the server before it's results are outputted to the browser, so for instance;

CODE
<html>

echo $var;

</html>


the above code wouldn't work because the browser doesn't know your trying to use PHP, for this reason you must put all PHP code inside it's correct opening/closing tags;

[php]<?php
// PHP code here
?>[/php]

So that the server knows it must process all the information within those tags before it sends it along to the browser.

If you want to mix HTML with PHP you'd need to put the HTML inside the PHP and echo it, like so;

[php]<?php

$var = $_GET['input']; // grab input from form.

// echo the HTML with the variable inserted within the code.
echo '<a href="http://www.areacodehelp.com/cgi-bin/weather/weather.cgi?pands=' .$var. '" target="_blank"><img src="http://www.areacodehelp.com/cgi-bin/weather/weather.cgi?config=png&forecast=zone&alt=hwizone&pands=' .$var. '&hwvbg=FFFFFF&hwvtc=B5OCOO&hwvusettf=0" alt="Zone Forecast for ' .$var. '" border="0" /></a>';

// you replace all instances of where the city or county would be in the code and replace it with the variable that contains the user inputted data.

?>[/php]

As for encorporating PHP into your form, I would need to see your form code first as you need to grab the data and store it in variables.

QUOTE
when it says //set the variables why does $var1=cookie1 and $var2=$_GET['input']; and so fourth. Why dosent it go $var1=cookie1 $var2 = cookie2.


When you write PHP, you always end a line of code with a semi-colon, this is so the server knows where the breaks are, so;

[php]<?php
$var1 = "text1"; // semi-colon ends line
$var2 = $_GET['input']
$var3 = 45; // no semi-colon on the line above, server cannot read and throws error
?>[/php]

All lines that you see starting with // are comments.

Hope this helps you a little more. Thanks for your kind words, much appreciated smile.gif
jesse
Hey Joe,
Thanks for helping me out. Right now im trying to figure out the best way to do this. I have a few things that i want to incorporate in the cookie that would be executed from one piece of information. But for now im going to focus on the one that i think is going to be the most complicated. So the way you have the following php and html working is by echoing it. Im starting to get the idea. So the variables would be the cities. I am wondering is there a way that i could attach a meaning to the cities. For example if someone were to enter in "Colorado state University" into a form the php would put or echo "fort collins" into the php where the $var. is.

QUOTE
[php]<?php

$var = $_GET['input']; // grab input from form.

// echo the HTML with the variable inserted within the code.
echo '<a href="http://www.areacodehelp.com/cgi-bin/weather/weather.cgi?pands=' .$var. '" target="_blank"><img src="http://www.areacodehelp.com/cgi-bin/weather/weather.cgi?config=png&forecast=zone&alt=hwizone&pands=' .$var. '&hwvbg=FFFFFF&hwvtc=B5OCOO&hwvusettf=0" alt="Zone Forecast for ' .$var. '" border="0" /></a>';

// you replace all instances of where the city or county would be in the code and replace it with the variable that contains the user inputted data.

?>[/php]


The form that i want to use is from one of your tutorials. I messed around with it to come up with something that looks like something I would want to use but have no idea how to make it work.
CODE
<?php
setcookie("weather");
setcookie("schoollink");
?>
<form method="post" name="cookie" action="process.php">

<p>
  <label for="username">Enter the name of your University Or College. <br>
  (ex:Colorado State University)
: <input type="text" name="username" id="username" /></label></p>

  <label for="setcookie"><input type="checkbox" name="setcookie" value="setcookie" " /> Remember My Homepage</label>
</p>

<p><input type="submit" name="submit" value="Submit" /> <input type="reset" name="reset" value="Reset" /></p>
</form>


I have been really busy this past week so i havent been able to respond but im still working on it an hopefully after im done training at my new job i can get this thing completed. Thanks again for any help with this.
Jesse
Joe
Jesse,

First things first;

QUOTE
I am wondering is there a way that i could attach a meaning to the cities. For example if someone were to enter in "Colorado state University" into a form the php would put or echo "fort collins" into the php where the $var. is.


This is only possible if you tell PHP that "Colorada State University" means "Fort Collins", so for this you'd need to declare every possibility of user input and match it to something. What you would be better off doing, is use a drop-down list in your form for the user to pick from a list what school or area etc they want.


QUOTE
The form that i want to use is from one of your tutorials. I messed around with it to come up with something that looks like something I would want to use but have no idea how to make it work.


Have you ever used PHP and done any kind of form processing before? Once you submit the form you need to grab and process the form data, from there you can then put your data inside a cookie. So for instance;

CODE
<?php if(!isset($_POST['submit'])) { // If the form hasn't been submitted, display the form. ?>

<form method="post" action="<?=$_SERVER['PHP_SELF'];?>">

<p><label for="thing">Foo: <br />
<input type="text" name="thing" id="thing" /></label></p>

<p><input type="submit" name="submit" value="Submit" /> <input type="reset" name="reset" value="Reset" /></p>
</form>

<?php } else { // If it has been submitted, process the form.

$var = $_GET['thing']; // Grab the form value (what the user entered into the text box).

setcookie("Cookie[Foo]", $var);
// Set the cookie.  ("Cookie name[array value]", content)

} ?>


Something like the above as an example.
jesse
Hey Joe,
I have no experience with form processing. I have been searching around the net for diffrent examples and stuff. I found a niffty one in html that i incorporated into my site. Its not a form but a drop down list. Anyways i think a drop down list for what we have been talking about would be the best approach as well. I edited the code above with some other code that i have found to come up with something that starting to look like something. (at least thats what im hoping) heres what i have come up with...
CODE
<?php if(!isset($_POST['submit'])) { // If the form hasn't been submitted, display the form. ?>

<form method="post" action="<?=$_SERVER['PHP_SELF'];?>">

<p><label for="thing">University or College: <br />
<select name="type_name" id="type_name">
<OPTION VALUE="">Select your school
      
              <?php
              do
                  {
                echo "<option value=\"" .$get_query['type_id']."\">".$get_query['type_name']."</option>";          
                }
            while ($get_query = mysql_fetch_assoc($query_two));
            ?>
      </select> </label>
</p>

<p><input type="submit" name="submit" value="Submit" /> </p>
</form>

<?php } else { // If it has been submitted, process the form.

$var = $_GET['thing']; // Grab the form value (what the user entered into the text box).

setcookie("Cookie[Foo]", $var);
// Set the cookie.  ("Cookie name[array value]", content)

} ?>


Once again im pretty sure it has no real function and im not advanced enough to understand the language. I just do trial and error until it looks like somthing that could work. From what i have been seeing is that this might need mysql in order to work. If thats the case how would this work? would the php call for the html relavent to the school that they chose from the drop down list? Thanks again for your help.
Jesse
Joe
Jesse,

You are right, you do need MySQL to use the drop down list that you want to encorporate into your form, so the first thing you need to do is create your database in PHPMyAdmin. From there you can add the following SQL code to create your table;

CODE
CREATE TABLE School (
ID int(11) NOT NULL auto_increment,
School varchar(255),
KEY ID (ID)
);

INSERT INTO School VALUES (1, 'Colorado State University, Fort Collins');


What you have here is a database table which will hold 2 fields: -
  • a unique identifier for each entry into the table,
  • and the name of the school.

If you are unfamiliar with databases you should look at bassreks tutorial, it should help you understand what is going on. So once you have populated your table you can then sort out your connection code.

So, altogether your PHP and form should look something like;

[php]<?php

$dbHost = 'localhost'; // Database Host
$dbUser = 'username'; // Database Username
$dbPass = 'password'; // Database Password
$dbName = 'table_name'; // Database Name

$db = mysql_connect("$dbHost","$dbUser","$dbPass"); // Connect to the database.
if (!$db) { die('Error : ' . mysql_error()); } // Database connection error checking.

$select_db = mysql_select_db($dbName,$db); // Select database.
if (!$select_db) { die('Error : ' . mysql_error()); } // More error checking.

if(!isset($_POST['submit'])) { // If the form hasn't been submitted, display the form.

?>

<form method="post" action="<?=$_SERVER['PHP_SELF'];?>">

<p><label for="thing">Foo: <br />
<select name="foo" id="foo"><?php
$sql = "SELECT ID, School FROM School"; // Select all information from the database table.
$result = mysql_query($sql, $db);
while($row = mysql_fetch_assoc($result)) {
$ID = $row['ID']; // Put data inside variables.
$School = $row['School'];
echo '<option value="' .$ID. '">' .$School. '</option>';
}
?></select></label>
</p>

<p><input type="submit" name="submit" value="Submit" /> </p>
</form>

<?php } else { // If it has been submitted, process the form.

$var = $_GET['foo']; // Get user choice from select box and assign variable.

setcookie("Cookie[Foo]", $var); // Put variable inside cookie.

} ?>[/php]

Do you have anything online to work with? Any PHP test scripts or something? It may help to know what things you have worked with or are working on which could help you get this script implemented.

Hope this helps.
jesse
Hey Joe,
I have been trying to check back here for a couple of days but the forum was down or something. Anyways this looks very interesting. Im going to mess areound with all of this and see what i can do. One thing i noticed is u labled the form "foo" can i change that to what ever i want? and if i do do that do i have to chage foo where ever i see it in the code?

CODE
<p><label for="thing">Foo: <br />
<select name="foo" id="foo"><?php


CODE
$var = $_GET['foo']; // Get user choice from select box and assign variable.

setcookie("Cookie[Foo]", $var); // Put variable inside cookie.


Thanks again for your all help.
Waleed
You can call it whatever you want, Jesse. "foo" is just a temp. name. But yes, do change foo to whatever you change to everywhere in the code.
jesse
Hey guys,
Its been a busy couple of weeks, school, work and so fourth. Anyways I have been trying to get this implimented and have a couple of questions. I have created a data base and implimented the code in the table.I also put all the necassary information in the php code such as the database name and password and so on. Heres what i have done with the code so far.

<?php

$dbHost = 'campusgo_campusgoal'; // Database Host
$dbUser = 'campusgo_campusg'; // Database Username
$dbPass = '*******'; // Database Password
$dbName = 'campusgo_campusgoal'; // Database Name

$db = mysql_connect("$dbHost","$dbUser","$dbPass"); // Connect to the database.
if (!$db) { die('Error : ' . mysql_error()); } // Database connection error checking.

$select_db = mysql_select_db($dbName,$db); // Select database.
if (!$select_db) { die('Error : ' . mysql_error()); } // More error checking.

if(!isset($_POST['submit'])) { // If the form hasn't been submitted, display the form.

?>

<form method="post" action="<?=$_SERVER['PHP_SELF'];?>">

<p><label for="thing">Choose your school: <br />
<select name="foo" id="foo"><?php
$sql = "SELECT ID, School FROM School"; // Select all information from the database table.
$result = mysql_query($sql, $db);
while($row = mysql_fetch_assoc($result)) {
$ID = $row['ID']; // Put data inside variables.
$School = $row['School'];
echo '<option value="' .$ID. '">' .$School. '</option>';
}
?></select></label>
</p>

<p><input type="submit" name="submit" value="Submit" /> </p>
</form>

<?php } else { // If it has been submitted, process the form.

$var = $_GET['choose your school']; // Get user choice from select box and assign variable.

setcookie("Cookie[choose your school]", $var); // Put variable inside cookie.

} ?>

not sure if i should sho the db name but if not i can create a new one. Anyways i am wondering where do i put the variables in the actual drop down? also how would i put "Choose your school" as the deafult caption in the drop down menu? so it would say choose your school and then when you hit the drop down arrow you would see the list of diffrent schools. Also i tried the code how it is now and when i hit the submit button it brings up this error message.

QUOTE

404 Not Found

The requested URL /%3C?=$_SERVER['PHP_SELF'];?%3E does not exist.

I am wondering if i need a seperate php file in my ftp manager where the index page is. Thanks again for all your help with this guys.
Jesse
Joe
Do you have your database set up and all your data within it?
jesse
no all i have in there right now is the one you posted,
CODE
CREATE TABLE School (
ID int(11) NOT NULL auto_increment,
School varchar(255),
KEY ID (ID)
);

INSERT INTO School VALUES (1, 'Colorado State University, Fort Collins');


i was trying to test it like this and if it worked i would start adding more. So i guess what i need is the data that im missing? This being the html for the weather (the whole pupose of the cookie). the rest im not sure. Im also wondering where do i put the actual selections in the drop down. Does the code call for the available selections from my data base when you hit the drop down button or do i put the selections in the php code? also do i need to do somthing with this
CODE
<?=$_SERVER['PHP_SELF'];?>">
do i need to chage php_self or i guess what im asking is what is PHP_SELF?
Joe
http://us3.php.net/reserved.variables

QUOTE
'PHP_SELF'

The filename of the currently executing script, relative to the document root. For instance, $_SERVER['PHP_SELF'] in a script at the address http://example.com/test.php/foo.bar would be /test.php/foo.bar. The __FILE__ constant contains the full path and filename of the current (i.e. included) file.


You need to run the SQL query inside your database to create the tables and insert the test data. From there you will be able to run your php to grab all data from the database to populate the drop down menu.
jesse
It has been awhile since i have worked on this due to the sheer complexity of it. However I believe my site wont start to gain a substantial amount of users until i do. Ok, so for anyone willing to help me on this many thanks in advance. we left off with the php code and building a database. I am currently trying to put the data in the database in phpmyadmin. This is where i get lost. So i am looking at the phpmy admin and at the top there is field, Type, Function, Null and value. Under this is Id, Int(11), then a drop down list under funtion and then value. so it looks sorta like this

Field Type Function Null Value

ID int(11) Drop Down No Action Blank


School varchar(255) Drop Down Check mark Blank

Sorry if this is kind of crude but its the best for me visually to explain it. Ok so heres my question for me to populate info in my drop down where would i put that? Im thinking the school blank value. So i could put Colorado in the blank box and that would be for that school. Then would i create another row and put Colorado State University in that one? I know i can figure this out but its kind of daunting. Once again thanks for any help with this.
Waleed
Welcome back, jesse!

Yes, you should fill in the boxes under "Value." But leave the box in front of "Id" blank. The database will add the numbers automatically.
jesse
Thank you, its good to be back. So i have entered in the three schools I want to start out with. This is how they look in the table. So from here i guess what i need to figure out is, how does this fit into my drop down menu? And once i get the drop down menu to populate these choices how does the table use the html and cookies that i want the script to set.


ID School
1 (1, 'Colorado State University, Fort Collins');
9 (3, 'University of Northern Colorado, Greeley');
8 (2, 'Colorado University, Boulder');


I looked back and saw what joe had written earlier about setting cookies. shown here

CODE
<?php
// Set the variables.
$var1 = "cookie1";
$var2 = $_GET['input'];
$var3 = 45;

// Store the variables inside cookies.
setcookie("Cookie[One]", $var1);
setcookie("Cookie[Two]", $var2);
setcookie("Cookie[Three]", $var3);

// Output the variables.
echo $_COOKIE[Cookie]['One'];
echo $_COOKIE[Cookie]['Two'];
echo $_COOKIE[Cookie]['Three'];
?>


so then would i attach a variable to each piece of html so that they could be used by the script? and would this list of different pieces of html reside inside the php script or in the table? Thanks again.
Waleed
To get the feilds from the database into a dropdown box, use the following code,

CODE
<?php
$sql = "SELECT * FROM School ORDER BY School";
$result = mysql_query($result);

$dropdown   = '<select name="weather">';
while ($row = mysql_fetch_array($result))
{
    $dropdown .= '<option value="'. $row['Id'] .'">' . $row['School'] . '</option>';
}
$dropdown .= '</select>';

echo $dropdown;
?>


EDIT: Oh. Joe has already posted this code in a previous post.
jesse
Thanks again for helping me out. I put the code for getting the choices from the database inside the php that will hopefully get this thing up and running. I also created a testing page here to test this thing out. I have the php code in the page and when you click the drop down its an empty box. Im not sure if the lables are wrong and its not getting the choices from the database.


ID School
1 (1, 'Colorado State University, Fort Collins');
9 (3, 'University of Northern Colorado, Greeley');
8 (2, 'Colorado University, Boulder');

This is what my table looks like. Any thoughts on what might be going wrong?
Waleed
Save the file as .php, not .html. So phptest.html should be phptest.php. Then it will work. smile.gif
jesse
Sweet! Now were getting somewhere. I saved it as phptest.php here and its working. Now i just need to play around with it and see what i can do. quick question though. Im still confused about the cookies part. Ive been searching the net for some info but havent found anything specific. So earlier joe posted this

CODE
<?php

$var = $_GET['input']; // grab input from form.

// echo the HTML with the variable inserted within the code.
echo '<a href="http://www.areacodehelp.com/cgi-bin/weather/weather.cgi?pands=' .$var. '" target="_blank"><img src="http://www.areacodehelp.com/cgi-bin/weather/weather.cgi?config=png&forecast=zone&alt=hwizone&pands=' .$var. '&hwvbg=FFFFFF&hwvtc=B5OCOO&hwvusettf=0" alt="Zone Forecast for ' .$var. '" border="0" /></a>';

// you replace all instances of where the city or county would be in the code and replace it with the variable that contains the user inputted data.

?>


would this be in the php code for the drop down list, so maybe somthing like this?

CODE
[php]<?php

$dbHost = 'localhost'; // Database Host
$dbUser = 'username'; // Database Username
$dbPass = 'password'; // Database Password
$dbName = 'table_name'; // Database Name

$db = mysql_connect("$dbHost","$dbUser","$dbPass"); // Connect to the database.
if (!$db) { die('Error : ' . mysql_error()); } // Database connection error checking.

$select_db = mysql_select_db($dbName,$db); // Select database.
if (!$select_db) { die('Error : ' . mysql_error()); } // More error checking.

if(!isset($_POST['submit'])) { // If the form hasn't been submitted, display the form.

?>

<form method="post" action="<?=$_SERVER['PHP_SELF'];?>">

<p><label for="thing">Foo: <br />
<select name="foo" id="foo"><?php
$sql = "SELECT ID, School FROM School"; // Select all information from the database table.
$result = mysql_query($sql, $db);
while($row = mysql_fetch_assoc($result)) {
$ID = $row['ID']; // Put data inside variables.
$School = $row['School'];
echo '<option value="' .$ID. '">' .$School. '</option>';
}
?></select></label>
</p>

<p><input type="submit" name="submit" value="Submit" /> </p>
</form>

<?php } else { // If it has been submitted, process the form.

$var = $_GET['foo']; // Get user choice from select box and assign variable.

setcookie("Cookie[Foo]", $var); // Put variable inside cookie.

<?php

$var = $_GET['input']; // grab input from form.

// echo the HTML with the variable inserted within the code.
echo '<a href="http://www.areacodehelp.com/cgi-bin/weather/weather.cgi?pands=' .$var. '" target="_blank"><img src="http://www.areacodehelp.com/cgi-bin/weather/weather.cgi?config=png&forecast=zone&alt=hwizone&pands=' .$var. '&hwvbg=FFFFFF&hwvtc=B5OCOO&hwvusettf=0" alt="Zone Forecast for ' .$var. '" border="0" /></a>';

// you replace all instances of where the city or county would be in the code and replace it with the variable that contains the user inputted data.


} ?>[/php]


I have been working with html for so long that this is how i think it should work but if im wrong any help would be appreciated. And thanks again for all the help thus far. thumbsupsmileyanim.gif
Waleed
Yeah, that's it. Just remove [php] from the beginning and end of the above code and then try it and see. smile.gif

One thing though: in your database, the school names are entered incorrectly. Rename the School fields so that this:
QUOTE
ID School
1 (1, 'Colorado State University, Fort Collins');
9 (3, 'University of Northern Colorado, Greeley');
8 (2, 'Colorado University, Boulder');
Looks like this:
QUOTE

ID School
1 Colorado State University, Fort Collins
9 University of Northern Colorado, Greeley
8 Colorado University, Boulder


To explain, edit (1, 'Colorado State University, Fort Collins'); and change it to Colorado State University, Fort Collins.

Is that the complete PHP code for your file phptest.php? Post the code around line 109...

Also, delete phptest.html for safety reasons...delete it and then I'll tell you what was wrong...
jesse
Ok, so I deleted the phptest.html file, i figure that was because with html prefix the browsers wouldnt hide the database and the password to it like it does with .php. correct? Also i created a new testing page here because i figured that to truly test this out i should be using the page i want to incorporate it in. With the new page i put the php code for the weather into the box i want it to show up in. This is how i think it should work. I tried using the code above but it kept throwing this error.
QUOTE
Parse error: syntax error, unexpected '}' in /home/campusgo/public_html/phptest2.php on line 378


I deleted the opening and closing php tags for the weather so the page would load and so you could see what im trying to do. Also in the original html code for the weather as shown here

CODE
<!--Weather image provided by www.areacodehelp.com--><a href="http://www.areacodehelp.com/cgi-bin/weather/weather.cgi?pands=greeley, co" target="_blank"><img src="http://www.areacodehelp.com/cgi-bin/weather/weather.cgi?config=png&forecast=zone&alt=hwizone&pands=greeley, co&hwvbg=FFFFFF&hwvtc=B5OCOO&hwvusettf=0" alt="Zone Forecast for Greeley, CO" border="0" /></a>


They use just the city for displaying the weather. In my table i have the school and city. I still want to have the schools in the drop down but need just the city to display for the variables in the php code. Sorry if this is kind of confusing. So what im thinking is that i need to do something with my table so that it only puts the city in for the variable. This is exciting because i have wanted this for my site for so long and its starting to come together. Thanks again for all your help. w00t.gif
Waleed
Post or attach the (complete) code for phptest2.php. I'll clean it up for you as well... smile.gif

Ok, for the setting up of this code...you can use either the city or the zipcode right? So, run the following SQL:
CODE
ALTER TABLE School ADD zip INT NOT NULL;


Then, get the zipcodes of the schools and save them to the database. Then, replace
CODE
$sql = "SELECT * FROM School ORDER BY School";
$result = mysql_query($result);

$dropdown   = '<select name="weather">';
while ($row = mysql_fetch_array($result))
{
    $dropdown .= '<option value="'. $row['Id'] .'">' . $row['School'] . '</option>';
}
$dropdown .= '</select>';

echo $dropdown;


with,
CODE
<form method="post" action="<?=$_SERVER['PHP_SELF'];?>">

<p><label for="weather">Select Your School: <br />
<?php
$sql = "SELECT * FROM school ORDER BY City";
$result = mysql_query($sql);

$dropdown   = '<select name="weather">';
while ($row = mysql_fetch_assoc($result))
{
    $dropdown .= '<option value="'. $row['City'] .'">' . $row['School'] . '</option>';
}
$dropdown .= '</select>';

echo $dropdown;
?>
</label>
</p>

<p><input type="submit" name="submit" value="Submit" /></p>
</form>


And,
CODE
<?php } else { // If it has been submitted, process the form.

$var = $_GET['foo']; // Get user choice from select box and assign variable.

setcookie("Cookie[Foo]", $var); // Put variable inside cookie.

<?php

$var = $_GET['input']; // grab input from form.

// echo the HTML with the variable inserted within the code.
echo '<a href="http://www.areacodehelp.com/cgi-bin/weather/weather.cgi?pands=' .$var. '" target="_blank"><img src="http://www.areacodehelp.com/cgi-bin/weather/weather.cgi?config=png&forecast=zone&alt=hwizone&pands=' .$var. '&hwvbg=FFFFFF&hwvtc=B5OCOO&hwvusettf=0" alt="Zone Forecast for ' .$var. '" border="0" /></a>';

// you replace all instances of where the city or county would be in the code and replace it with the variable that contains the user inputted data.


} ?>


replace with,
CODE
<?php } else { // If it has been submitted, process the form.

$var = $_GET['weather']; // Get user choice from select box and assign variable.

setcookie("Cookie[weather]", $var); // Put variable inside cookie.

// echo the HTML with the variable inserted within the code.
echo '<a href="http://www.areacodehelp.com/cgi-bin/weather/weather.cgi?pands=' .$var. '" target="_blank"><img src="http://www.areacodehelp.com/cgi-bin/weather/weather.cgi?config=png&forecast=zone&alt=hwizone&pands=' .$var. '&hwvbg=FFFFFF&hwvtc=B5OCOO&hwvusettf=0" alt="Zone Forecast for ' .$var. '" border="0" /></a>';
// you replace all instances of where the city or county would be in the code and replace it with the variable that contains the user inputted data.

} ?>
jesse
So I added a new row in the table so it looks like this as follows.




QUOTE
ID School zip
1 Customize Campusgoal to your School 0
9 University of Northern Colorado, Greeley 80631
8 Colorado University, Boulder 80301
10 Colorado State University, Fort Collins 80521
I also replaced the php code with the ones you posted and it threw this error.

QUOTE
Parse error: syntax error, unexpected '}' in /home/campusgo/public_html/phptest2.php on line 379


I left the page like this so you could see what is going on with it. I have also attached the phptest.php file as well. Also while were doing this in the beginning of this post I also wanted to use this drop down to be able to save a link to the different schools. so for instance

CODE
<td><a href="http://www.unco.edu"> <img src='images/unco.jpg'"/></a></td>


brings up this image with a link to my schools website. I would also like to do this for every school. So from what i have been seeing to do this i would need to add two more rows to my database. One for the web address and one for the image. So i would run this sql
CODE
ALTER TABLE School ADD Address INT NOT NULL; ALTER TABLE School ADD Image INT NOT NULL;


Then in my links box there would be something like this?


CODE
<?php }
else {
$var = $_GET['address'];

setcookie("Cookie[address]", $var);

echo <td><a href="' .$var. '"> <img src=' .$var. '"/></a></td>


} ?>


Im wondering if the td tags would be on the outside of the php so it would fit into the table correctly. Once again thanks for all your help.
jesse
Hey guys, I have been messing around getting a better feel for all of this. Im a lot more familiar with it then i was little bit a ago. So i ran this sql
CODE
ALTER TABLE School ADD Address INT NOT NULL; ALTER TABLE School ADD Image INT NOT NULL;

and it added two more rows. So when i try and add say www.unco.edu into address and unco.jpg into image it dosent save? I am wondering why that is. Also Im still a little confused how the cookie knows what var to grab?

CODE
$var = $_GET['weather']; // Get user choice from select box and assign variable.

setcookie("Cookie[weather]", $var); // Put variable inside cookie.



so in this snipit of the php code the code is telling the browser to grab and set the cookie "weather". How does it know that the var is the zipcode? Thanks again. biggrin.gif
Waleed
Okay, here's the cleaned php file...I've only touched the PHP...
Click to view attachment


Run this SQL query...the problem is the data type set for the new rows...the following will change the data type from INT (integer) to VARCHAR (various characters, I think)......
CODE
ALTER  TABLE school  CHANGE Address Address VARCHAR(255)  NOT  NULL,
CHANGE Image Image VARCHAR(255)  NOT  NULL


Now try adding the adress and image address...

QUOTE
Also Im still a little confused how the cookie knows what var to grab?


It says, $var = $_GET['weather']; right. The $_GET['weather'] part tells the php to get the variable from the form field named "weather" which is our dropdown...This way, it will grab the value which is inside <option value= which is in this case the zip code...

CODE
<option value="'. $row['zip'] .'">' . $row['School'] . '</option>'

jesse
Hey waleed,
Thanks again for taking the time to do that. I used the script and put it up on the phptest2.php. the drop down dosent seem to populate the list. Any thoughts on why that might be? Thanks again.
Joe
Can you export your SQL database and post the contents here so I can take a look smile.gif
Waleed
Did you fill in the correct database information? Double check those, it could be because of that. And BTW, your table is named "School" or "school"? That could make a difference also...I might have mistakenly typed the wrong case...check that also...
jesse
I went through and double checked the database and the user information and it all checks out. I also tried to go through the code and change all the schools to Upper case. Im going to go through and check again and see what i can muster. Also here is the export of my database below if that helps.

CODE
phpMyAdmin SQL Dump
-- version 2.9.0.2
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Jan 27, 2007 at 08:07 PM
-- Server version: 4.1.21
-- PHP Version: 4.4.2
--
-- Database: `campusgo_campusgoal`
--

-- --------------------------------------------------------

--
-- Table structure for table `School`
--

CREATE TABLE `School` (
  `ID` int(11) NOT NULL auto_increment,
  `School` varchar(255) default NULL,
  `zip` int(11) NOT NULL default '0',
  `Address` varchar(18) NOT NULL default '0',
  `Image` varchar(11) NOT NULL default '0',
  KEY `ID` (`ID`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=15;

--
-- Dumping data for table `School`
--

INSERT INTO `School` VALUES (1, 'Customize Campusgoal to your School', 0, '0', '0');
INSERT INTO `School` VALUES (9, 'University of Northern Colorado, Greeley', 80631, 'www.unco.ed', 'unco.jpg');
INSERT INTO `School` VALUES (8, 'Colorado University, Boulder', 80301, 'www.colorado.edu', 'cu.jpg');
INSERT INTO `School` VALUES (10, 'Colorado State University, Fort Collins', 80521, 'www.colostate.edu', 'csu.jpg');


Thanks agian. thumbsupsmileyanim.gif
Joe
I've done a little bit of playing, your edited SQL;

CODE
CREATE TABLE School (
  ID int(11) NOT NULL auto_increment,
  School varchar(255) default NULL,
  Zip int(11) NOT NULL,
  Address varchar(100) NOT NULL,
  Image varchar(20) NOT NULL,
  KEY ID (ID)
);

INSERT INTO School VALUES (1, 'Customize Campusgoal to your School', 0, '0', '0');
INSERT INTO School VALUES (2, 'University of Northern Colorado, Greeley', 80631, 'http://www.unco.ed', 'unco.jpg');
INSERT INTO School VALUES (3, 'Colorado University, Boulder', 80301, 'http://www.colorado.edu', 'cu.jpg');
INSERT INTO School VALUES (4, 'Colorado State University, Fort Collins', 80521, 'http://www.colostate.edu', 'csu.jpg');


school.php
CODE
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />

    <title>Title</title>
<?php
$dbHost = 'localhost';
$dbUser = 'username';
$dbPass = 'password';
$dbName = 'db_name';

$db = mysql_connect("$dbHost","$dbUser","$dbPass");
if (!$db) { die('Error : ' . mysql_error()); }

$select_db = mysql_select_db($dbName,$db);
if (!$select_db) { die('Error : ' . mysql_error()); }
?>
</head>
<body>

<fieldset><legend>Select School</legend>
<form action="setcookie.php" method="post">

<?
echo '<p><label for="school"><strong>School : </strong></label><select name="school" id="school">'."\n";

$sql = "SELECT ID, School FROM School";
$result = mysql_query($sql, $db);
$count = mysql_num_rows($result);
if($count == 0)
{
    echo '<option>No Schools added yet.</option>';
}
while($row = mysql_fetch_assoc($result))
{
    echo '<option value="'.$row['ID'].'"';
    if($_COOKIE[Cookie]['SchoolID'] == $row['ID']) { echo ' selected'; }
    echo '>'.$row['School'].'</option>'."\n";
}
echo '</select>';

?><input type="submit" name="submit" value="Go" /></p>

</form>
</fieldset>

<p><?
$var = $_COOKIE[Cookie]['SchoolID'];
$sql = "SELECT ID, School, Address, Image FROM School WHERE ID = '$var'";
$result = mysql_query($sql, $db);
$count = mysql_num_rows($result);
while($row = mysql_fetch_assoc($result))
{
echo '<a href="'.$row['Address'].'" title="'.$row['School'].'"><img src="'.$row['Image'].'" /></a>';
}
?></p>

</body>
</html>


setcookie.php
CODE
<?php

$School = $_POST['school'];

setcookie("Cookie[SchoolID]", $School);

header('Location: school.php');

?>


Very similar to what you want I think, it should give you somewhere to work from at least smile.gif
Waleed
Yes, that should work like you want it! biggrin.gif
jesse
Thank you so much! It took a little trial and error but i got it to work here. Now im trying to make it look good. I want to make the back ground black for the drop down. I did some searching on the net for a code that would do this and came up with this.

CODE
<?
// use black as the background color
$bgcolor = '#000000';

// the text color is white
$textColor = '#ffffff';


echo '<p><label for="school"><strong>School : </strong></label><select name="school" id="school">'."\n";

$sql = "SELECT ID, School FROM School";
$result = mysql_query($sql, $db);
$count = mysql_num_rows($result);
if($count == 0)
{
    echo '<option>No Schools added yet.</option>';
}
while($row = mysql_fetch_assoc($result))
{
    echo '<option value="'.$row['ID'].'"';
    if($_COOKIE[Cookie]['SchoolID'] == $row['ID']) { echo ' selected'; }
    echo '>'.$row['School'].'</option>'."\n";
}
echo '</select>';

?><input type="submit" name="submit" value="Go" /></p>

</form>
</fieldset>


I Have tried this way and tried using html but it dosent want to catch. Best way i can describe it. Anyways Thank you again so much for all your help. I just showed my girlfriend what we have been doing and she thought it was sexy. biggrin.gif
Waleed
Try this CSS:
CODE
<style type="text/css">
option {
background-color: #000000;
font-size: 12px;
color: #ffffff;
}
</style>
Joe
So how is the server side going?

By the way, in your CSS you may want to set your margin, padding and border for your fieldset element to zero and hide the legend element.
jesse
Its going well. I think i need to do something with my dreamweaver so i can connect to my database with out having to ftp my edited code to see what it looks like. Other than that its been good. I have been busy trying to get my site looking good with this new addition. I tried the css
option{ and it made the actual drop down black. Which i thought looked kind of cool but decided it wasn't practical. So after a few hour of trying <td bgcolors> and some more css attempts I created a table around the php code inside my nav bar table and got something that will do, for now at least. But i have been messing around with the code you posted to try and utilize the zip codes in my table. After looking at it piece by piece I came up with this
CODE
<p><?
$var = $_COOKIE[Cookie]['SchoolID'];
$sql = "SELECT ID, Zip FROM School WHERE ID = '$var'";
$result = mysql_query($sql, $db);
$count = mysql_num_rows($result);
while($row = mysql_fetch_assoc($result))
{
echo '<a href="http://www.areacodehelp.com/cgi-bin/weather/weather.cgi?pands=' .$row['Zip']. '" target="_blank"><img src="http://www.areacodehelp.com/cgi-bin/weather/weather.cgi?config=png&forecast=zone&alt=hwizone&pands=' .$row['zip']. '&hwvbg=FFFFFF&hwvtc=B5OCOO&hwvusettf=0" alt="Zone Forecast for ' .$row['Zip']. '" border="0" /></a>';
}
?></p>


and posted it in my page where i wanted it to show up. All i see is their default city. Do i need to add another cookie to the mix?
Waleed
Could be a case-issue.

CODE
<img src="http://www.areacodehelp.com/cgi-bin/weather/weather.cgi?config=png&forecast=zone&alt=hwizone&pands=' .$row['zip']. '&hwvbg=FFFFFF&hwvtc=B5OCOO&hwvusettf=0" alt="Zone Forecast for ' .$row['Zip']. '" border="0" />


Should be,
CODE
<img src="http://www.areacodehelp.com/cgi-bin/weather/weather.cgi?config=png&forecast=zone&alt=hwizone&pands=' .$row['Zip']. '&hwvbg=FFFFFF&hwvtc=B5OCOO&hwvusettf=0" alt="Zone Forecast for ' .$row['Zip']. '" border="0" />
jesse
yaahhhooooo! ariba ariba, i dont even speak a spanish. Thank you guys so much. It's working! Ahhh this is so awesome. Joe i wish i had your walking guys going the opposite way so hes doing the moon walk because thats what i feel like doing right now. And Waleed Thank you again, I went surfing through you double u design site and i love how its written and presented to customers, very well done indeed. cool.gif w00t.gif specool.gif thumbsupsmileyanim.gif
Waleed
specool.gif

Glad it's working like it was planned, jesse! After you're encounter with PHP, if you're still interested in tackling the monster I'd like to forward you to this set of tutorials that is a great way of learning how it works.

QUOTE
And Waleed Thank you again, I went surfing through you double u design site and i love how its written and presented to customers, very well done indeed.


Thanks very much! biggrin.gif
Joe
Glad everything is working for your jesse smile.gif

While Waleed is forwarding you away from our forum, I'd like to mention that we have plenty of PHP tutorials available here for your enjoyment biggrin.gif
Waleed
QUOTE(Joe @ Feb 1 2007, 12:49 AM) [snapback]35678[/snapback]
I'd like to mention that we have plenty of PHP tutorials available here for your enjoyment biggrin.gif


Indeed!
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.