Forum Moderators: coopster

Message Too Old, No Replies

Trip Report PHP

         

outdoorxtreme1

5:02 pm on Oct 18, 2005 (gmt 0)

10+ Year Member



I am a newbie to PHP. How would I go about creating a trip report submission.

Any help would be greatly appreciated. I want to create trip reports for my canoe club webpage.

Thanks in advance.

Dustin

[edited by: jatar_k at 5:28 pm (utc) on Oct. 18, 2005]
[edit reason] no urls thanks [/edit]

jatar_k

4:32 pm on Oct 25, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



not quite

you need to take your query and fire it off to mysql before your while loop

$connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>");
mysql_select_db($dbname);

$sql = 'SELECT * FROM tripreport';
$result = mysql_query($sql) or die ('<p>select died: ' . mysql_error());

$counter = 1;
while ($row = mysql_fetch_array($result)) {
echo "<p>$counter: <pre>";
print_r($row);
echo '</pre>';
$counter++;
}

outdoorxtreme1

4:47 pm on Oct 25, 2005 (gmt 0)

10+ Year Member



Ok, cool that worked. I really appreciate all the help. It is teaching me some new things.

Ok, now I want to make a page that will display only certain rows from each trip report entered. I want it to display row 0,6,7,8 for the full list and how would I place a link on the data from row 0 to another page that showed all the info for just that trip nad not the full list.

jatar_k

4:49 pm on Oct 25, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



what criteria do you have for those records?

are those trip ids?

stored in a column in the table?

what is the column name?

is there anything else that is alike between the records you want to display?

outdoorxtreme1

5:00 pm on Oct 25, 2005 (gmt 0)

10+ Year Member



I want the "triplocation", "TSpickMonth", "TSpickDay", "TSpickYear" to display on one page.

Example of what page would look like:
******

Trip Reports

Clarion River - October 19, 2005
Mahoning River - November 12, 2005
Mahoning River - November 21, 2005

******
Would like to somehow be able to click on each and see all the information entered to mysql for each trip.

jatar_k

5:07 pm on Oct 25, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



but is there some unique identifier for each row?

outdoorxtreme1

5:12 pm on Oct 25, 2005 (gmt 0)

10+ Year Member



Not right now. Could I use an invisible id number somehow? How would that work with my submit html

jatar_k

5:22 pm on Oct 25, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I would create a column for your unique id something like

ALTER TABLE tripreport ADD COLUMN trip_id int(4) AUTO_INCREMENT FIRST

ALTER TABLE syntax
[dev.mysql.com...]

when you insert a row that has an auto_increment field you can just insert a blank with 2 single quotes ''

the field itself will take care of what the next value is

this unique number can then be used to build your links and select individual rows

outdoorxtreme1

5:30 pm on Oct 25, 2005 (gmt 0)

10+ Year Member



In mysql, what "type" should all my fields be. I think this might be one of my problems.

jatar_k

5:40 pm on Oct 25, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



they should be a type that reflects the type of data it is storing

yeah, not the answer you may have been looking for but it is the right answer ;)

here are some links
MySQL Column Types [dev.mysql.com]
most of the time I am concerned with these 2
Overview of Numeric Types [dev.mysql.com]
Overview of String Types [dev.mysql.com]

outdoorxtreme1

5:45 pm on Oct 25, 2005 (gmt 0)

10+ Year Member



ok, I have all of that done. What commands do I use to show the page how I described above?

jatar_k

6:01 pm on Oct 25, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



well you need to only select the columns you need, plus always selct the trip_id

SELECT trip_id, triplocation, TSpickMonth, TSpickDay, TSpickYear from tripreport

I didn't add the WHERE clause to this yet, which would only select certain rows

now these will be available in our while loop. They will be in the $row array and will be accessible using the column names, like so

echo '<p>Trip Reports';
while ($row = mysql_fetch_array($result)) {
echo '<br>Trip ',$row['trip_id'],': ',$row['triplocation'],' - ',$row['TSpickMonth'],' ',$row['TSpickDay'],', ',$row['TSpickYear '];
}

all I did was replace the strings you had with their variables. I also added Trip with trip_id to the front just for a little clarity.

outdoorxtreme1

6:11 pm on Oct 25, 2005 (gmt 0)

10+ Year Member



ok. that works good. How would I add a link to the "triplocation" field so that it will go to another page and display all the columns for that "trip_ID" row

outdoorxtreme1

11:27 am on Oct 26, 2005 (gmt 0)

10+ Year Member



I tried messing with the code I have so far and couldn't figure out how to get it to do what I want. I would like to be able to click on one of the trip names then have it go to another page where it will display about 22 different column entries for a certain trip_id

outdoorxtreme1

4:06 pm on Oct 26, 2005 (gmt 0)

10+ Year Member



I have this so far. Can anyone tell me how to only display one row from MySQL with this code if I have another page with a list of rows to select from?

$connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>");
mysql_select_db($dbname);

$sql = 'SELECT * FROM tripreport';
$result = mysql_query($sql) or die ('<p>select died: ' . mysql_error());

while ($row = mysql_fetch_array($result)) {
echo '<div><strong>',$row['triplocation'],'</strong> - ',$row['TSpickMonth'],' ',$row['TSpickDay'],', ',$row['TSpickYear'],'<br>';
echo 'River Class: ',$row['riverclass'],'<br>';
echo 'Water Level: ',$row['rivercondition'],'<br>';
echo 'Trip Organizer: ',$row['triporganizer'],'<br>';
echo 'Solo Kayak: ',$row['k1'],'<br>';
echo 'Tandem Kayak: ',$row['k2'],'<br>';
echo 'Solo Canoe: ',$row['c1'],'<br>';
echo 'Tandem Canoe: ',$row['c2'],'<br>';
echo 'USGS Gauge Info: ',$row['GaugeID'],'<br>';
echo 'USGS Gauge Height: ',$row['GaugeFT'],' ft<br>';
echo 'USGS Flow: ',$row['GaugeCFS'],' cfs<br>';
echo '<br>';
echo '',$row['report'],'<br>';
echo '<br>';
echo '<a href="mailto:',$row['email'],'">',$row['author'],'</a><br>';
echo '<br>';
}

jatar_k

4:29 pm on Oct 26, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



for the link you can use the trip_id to append to it so on the next page you will be able to select a single row.

first we can build the link

echo '<div><strong><a href="tripdetail.php?trip=',$row['trip_id'],'">',$row['triplocation'],'</a></strong> - ',$row['TSpickMonth'],' ',$row['TSpickDay'],', ',$row['TSpickYear'],'<br>';

I don't know the name of the next script so I put tripdetail.php just change that to the right name

then in the next part you can access the passed id using $_GET['trip']. We then use that in our query for a single trip using a WHERE clause.

Now a moment for mentioning that we never trust user data. The id we are passing in the urll can easily be changed by the user so we need at least a tiny amount of security. In this case we will make sure it is numeric before we pull it into a new variable and use it in our query.

if (is_numeric($_GET['trip'])) {
$tripdetail = $_GET['trip'];
} else {
echo 'that is not a number';
die;
}

that is an ugly error handler and you should probably make it fail properly but for now it's fine. You will also need to handle the case that the number has been changed and your query returns nothing but that is for later as well.

then your query would be like

$sql = 'SELECT * FROM tripreport WHERE trip_id=' . $tripdetail;

outdoorxtreme1

4:43 pm on Oct 26, 2005 (gmt 0)

10+ Year Member



I am confused on waht page to put this on. I have the trip report list named - report_list.php and the the code I posted above is named report.php

outdoorxtreme1

5:06 pm on Oct 26, 2005 (gmt 0)

10+ Year Member



I finally got it! Thanks alot. Sorry for so many questions but I am pretty new to PHP. You taught me some new things.

outdoorxtreme1

5:12 pm on Oct 26, 2005 (gmt 0)

10+ Year Member



Do you happen to know an easy PHP command to go back a page?

outdoorxtreme1

5:49 pm on Oct 26, 2005 (gmt 0)

10+ Year Member



That was a dumb question. I figured it out. There is a question that I am not sure about though. When I submit a trip report using my html form it goes to the submit.php to enter the data into MySQL. How can I get it to go to another page from there?

ergophobe

5:59 pm on Oct 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I thought I posted in this thread... but I guess not and now it's gone quite a long ways.

If your goal is to learn PHP, by all means go for it.

If your goal is simply to get a trip report submission/management interface up, you might want to look at installing Drupal (that's a CMS... er, excuse me "community plumbing" is the term they prefer I believe) with the flexinode module. That would let you set up custom fields, make them required or not as needed, allow you to queue submissions from some users (e.g. trusted users) and queue submissions for admin approval from the rest, build menus on the fly and more.

I'm actually using it for pretty much that purpose on one site. I built a custom solution on another outdoor-oriented site where I'm contemplating the changeover to drupal actually. The only reason I haven't is 1) it's a thankless pro bono deal for a group I used to do a lot with and I don't want to put the time into themeing drupal 2) the custom site integrates with a reservation system that I also built which is not that complex, but people get real upset about any changes in it, so I'm waiting until it fails catastrophically before messing with it ;-)

outdoorxtreme1

6:16 pm on Oct 26, 2005 (gmt 0)

10+ Year Member



I tried to add this but it didn't seem to work right.

echo'<meta http-equiv="Refresh" content="0" URL="/tct/trip_reports.html">';

jatar_k

6:28 pm on Oct 26, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



or you can use the header [php.net] function

you have to make sure there is no output before you use this function though. Even a blank line before your first php tag causes output to the browser.

something like this maybe

header("Location: /tct/trip_reports.html");

though you may have to play with the path a bit

outdoorxtreme1

8:50 pm on Oct 26, 2005 (gmt 0)

10+ Year Member



Cool I have the basics of what I need. I just need to tweak a few things. Is there a way not to display a column if there is no data in it?

jatar_k

8:50 pm on Oct 26, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



it should show blank as you will echo an empty variable

outdoorxtreme1

8:56 pm on Oct 26, 2005 (gmt 0)

10+ Year Member



I would like the whole descripiton to not show up.

Say
on one row
Water level: medium

the next row this field is empty
Water level:

Is there a way to make the Description invisible if there is no data?

jatar_k

8:58 pm on Oct 26, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



test to see if the variable is empty using empty() [php.net] in an if else statement using not empty

an example being

$myvar = '';
if (!empty($myvar)) {
echo 'it is not empty';
} else {
echo 'your variable is empty';
}

outdoorxtreme1

9:07 pm on Oct 26, 2005 (gmt 0)

10+ Year Member



How would I use it for all of these fields? If there is not data entered I would like to have it not show any of the echo. Is this possible?

$sql = 'SELECT * FROM tripreport WHERE trip_id=' . $tripdetail;
$result = mysql_query($sql) or die ('<p>select died: ' . mysql_error());

while ($row = mysql_fetch_array($result)) {
echo '<div><strong>',$row['triplocation'],'</strong> - ',$row['TSpickMonth'],' ',$row['TSpickDay'],', ',$row['TSpickYear'],' to ',$row['TFpickMonth'],' ',$row['TFpickDay'],', ',$row['TFpickYear'],'<br>' ;
echo '<br>';
echo '<strong>River Class: </strong>',$row['riverclass'],'<br>';
echo '<strong>Water Level: </strong>',$row['rivercondition'],'<br>';
echo '<strong>Trip Organizer: </strong>',$row['triporganizer'],'<br>';
echo '<strong>Solo Kayak: </strong>',$row['k1'],'<br>';
echo '<strong>Tandem Kayak: </strong>',$row['k2'],'<br>';
echo '<strong>Solo Canoe: </strong>',$row['c1'],'<br>';
echo '<strong>Tandem Canoe: </strong>',$row['c2'],'<br>';
echo '<strong>USGS Gauge Info: </strong>',$row['GaugeID'],'<br>';
echo '<strong>USGS Gauge Height: </strong>',$row['GaugeFT'],' ft<br>';
echo '<strong>USGS Flow: </strong>',$row['GaugeCFS'],' cfs<br>';
echo '<br>';
echo '',$row['report'],'<br>';
echo '<br>';
echo '<a href="mailto:',$row['email'],'">',$row['author'],'</a><br>';
echo '<br>';
echo '<A href="javascript:history.back(1)">
Back</A>';
}

jatar_k

9:18 pm on Oct 26, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> Is this possible?

it's PHP, anything is possible my friend ;)

certain information must be required, I am guessing your first row of info (triplocation and date)

for the rest you could do an inline if for each similar to this

if (!empty($row['riverclass'])) echo '<strong>River Class: </strong>',$row['riverclass'],'<br>';

then just do the same for each line

outdoorxtreme1

11:18 am on Oct 27, 2005 (gmt 0)

10+ Year Member



If I have the same date for trip start and trip end, how do I only show one date.

Current example:

October 27, 2005 to October 27, 2005

Would like example:

October 27, 2005

jatar_k

3:11 pm on Oct 27, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I think I would concatenate your date strings together and then compare them

$startdate = $row['TSpickMonth'] . ' ' . $row['TSpickDay'] . ', ' . $row['TSpickYear'];
$enddate = $row['TFpickMonth'] . ' ' . $row['TFpickDay'] . ', ' . $row['TFpickYear'];

then break up the display line, always show the first part but only show the second part if they are not the same

echo '<div><strong>',$row['triplocation'],'</strong> - ', $startdate;

if ($enddate!= $startdate) echo ' to ',$enddate,'<br>' ;

really we should use strcmp [php.net] to cpmpa5re strings but I never do

This 158 message thread spans 6 pages: 158