PDA

View Full Version : How do you pass a value through the address URL?



kuau
07-18-2008, 10:03 AM
I have seen a lot of websites where the id value for the current record is passed on the end of the URL address. For example:

http://www.website.com/view_entry.php?id=23045

How do you get the view_entry.php file to be a file that will allow you to do this? Do you place something in the file to know to look there for a value? Or how do you get the file to run using that particular value?

How do you stick the bit on at the end (after the ?)?

I'd google it but don't know what it's called. Thanks.

allahverdi
07-18-2008, 11:00 AM
If i didn't understand you correctly sorry.

Then get that value with mysql.

SELECT * FROM myarticle WHERE id='$id';

then they get that row and etc.

codeexploiter
07-18-2008, 11:13 AM
The values passed as a part of the URL string is based on HTTP method GET. So you can use your server-side method for gathering the parameters like the normal GET method based ones.

In most of the server-side tools they have different methods for gathering parameters from the client in GET and POST methods. Anything that comes as a part of the URL string will be considered as a GET method.

Please find an example below based on PHP

filename: one.htm


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Untitled Document</title>
<style type="text/css">

</style>
<script type="text/javascript">

</script>
</head>
<body>
<a href="one.php?name=Code&age=33">PHP File</a>
</body>
</html>


filename: one.php


<?php
$name = $_GET['name'];
$age = $_GET['age'];

print "The parameters passed through URL are $name and $age";
?>


If you have a webserver in your PC then save these code in the mentioned names in the publishing root of your webserver, browse the HTM file and click the link and you can see how the parameters are passing.

Hope this helps.

kuau
07-18-2008, 11:44 AM
Yes! That is what I'm talking about! Here is the code that displays the link:


<a href="/php/event-detail.php?event_id=$event_id">

but when I click on it, the address URL looks like this:

http://www.calendarmaui.com/php/event-detail.php?event_id=$event_id

If I put a literal number instead of $event_id it works, but it is a list of different events so I have to use a variable. Thanks so much for helping me with this. It is a live site and I have to get it working asap.

codeexploiter
07-18-2008, 11:59 AM
<a href="/php/event-detail.php?event_id=$event_id">

Try to chanage the above one to the following.


<a href="/php/event-detail.php?event_id=<?=$event_id ?>">some link text</a>

Or


<a href="/php/event-detail.php?event_id=<?php print $event_id ?>">some link text</a>

Then see the link shows the value stored in $event_id in the URL or not.

kuau
07-18-2008, 12:18 PM
Dear Code:

Yes, that is almost it! But it put the same number on all of the links so I tried moving the display to inside the while loop and it seemed to fix the links sort of, but it wonked out something and made it take a long time. Would you mind please taking a look at the code & tell me where to put the bracket? It's only 52 lines.

Thanks a million. e :)

codeexploiter
07-18-2008, 12:37 PM
A link to your page or the source code needs to be furnished here for that.

Nile
07-18-2008, 12:38 PM
Check my post below.

kuau
07-18-2008, 12:42 PM
Any suggestions for improvement gratefully accepted.


<?php
putenv("TZ=Pacific/Honolulu");
include('cal-connectdb.php');

$today = date ('Y-m-d');

$query = " SELECT `event_id`, `startdate`, `event_title`
FROM `event`
WHERE `featured` = '1' AND `startdate` >= '".$today."'
ORDER BY `startdate`, `event_title` LIMIT 200 ";

$result = @mysql_query($query,$connection) or die("Couldn't execute $query query.");
$eventList = array();

while ($row = mysql_fetch_array($result))
{
$event = array();

$event_id = $row['event_id' ];

// Get details for each event

$query = "SELECT `event_title`, date_format(`startdate`, '%M %D, %Y') as date, time_format(`starttime`, '%h:%i %p') as time
FROM `event`
WHERE event_id = '".$event_id."' ";

$result2 = @mysql_query($query,$connection) or die("Couldn't execute $query query.");
$row2 = mysql_fetch_array($result2);

$event['event_title'] = $row2['event_title'];
$event['startdate'] = $row2['date'];
$event['starttime'] = $row2['time'];
$event['endtime'] = $row2['endtime'];

mysql_free_result($result2);
unset($row2);

array_push($eventList, $event);

}

//mysql_free_result($result);
//unset($row);

mysql_close($connection);
?>

<? foreach($eventList as $event) { ?>
<div class="event">
<a href="/php/event-detail.php?event_id=<?=$event_id?>"> <?=$event['event_title'];?></a><br><?=$event['startdate'];?> at <?=$event['starttime'];?>
</div><!--End event-->
<? } ?>

Nile
07-18-2008, 12:53 PM
Change line #50, to this:


<a href="/php/event-detail.php?event_id=<?=$event_id++;?>"> <?=$event['event_title'];?></a><br><?=$event['startdate'];?> at <?=$event['starttime'];?>

kuau
07-18-2008, 01:09 PM
Dear Nile: Thanks for that suggestion. It did change the numbers for the links but they are all wrong. It could not possibly work in this case because the events are not in consecutive order by id number. When I moved the foreach loop inside the while loop it did fix the links, so it was really close, but not quite right.

Do you happen to know the command to set the time format to lowercase am/pm?

$event['starttime'] = $row2['time'];

I want to do something like this: $event['starttime'] = time_format (hh:mm a, $row2['time'];) Thanks very much for helping.

Nile
07-18-2008, 01:11 PM
So, all you want is a lowercase PM and AM? Just use strtoupper. Search google for it.

kuau
07-18-2008, 01:15 PM
I thought that was for string data types. I desperately need to get the links fixed properly. The am/pm is merely aesthetic.

Codeexploiter, where art thou?

Nile
07-18-2008, 01:29 PM
Try it.

blm126
07-18-2008, 05:41 PM
Untested, but a lot cleaner than what you had


<?php
putenv("TZ=Pacific/Honolulu");
include('cal-connectdb.php');
$today = date ('Y-m-d');
$query = " SELECT `event_id`,`event_title`, date_format(`startdate`, '%M %D, %Y') startdate, time_format(`starttime`, '%h:%i %p') starttime
FROM `event`
WHERE `featured` = '1' AND `startdate` >= '".$today."'
ORDER BY `startdate`, `event_title` LIMIT 200 ";
$result = @mysql_query($query,$connection) or die("Couldn't execute $query query.");
while($event = mysql_fetch_assoc($result)){
echo "<div class='event'>
<a herf='/php/event-detail.php?event_id=".$event['event_id']."'>".$event['event_title.'"</a><br>".$event['startdate']." at ".$event['starttime']."
</div>";
}
?>

You seemed to have a lot of uneeded code in there.

kuau
07-18-2008, 05:46 PM
Thanks very much. I'll try it out and let you know. :)

kuau
07-19-2008, 09:26 AM
Dear Brady:

That was brilliant!! TheTestingSite fixed a couple of typos in it, but your code accomplished in a few lines what mine took a page to do. I know in my head the logic of what I want to accomplish, but don't know the language (php) well enough to express it. I *really* appreciate all the assistance. Was able to make the page live before everyone woke up.

Niles: what got the proper id numbers showing up for each event was changing the $event_id to $event['id']. TheTestingSite figured it out.

Thanks a million to all of you. e :)

GarethMc
07-21-2008, 08:50 AM
Just thought I would mention something. The use of the short form <?=$variable ?> is considered bad practice these days and in fact short tags <? and ?> instead of <?php and ?> are seriously discouraged due to conflicts with XML. I would recommend that you use <?php echo $variable ?> rather than that short form. Its only a few extra characters and makes your code conform to best practices.

codeexploiter
07-21-2008, 08:53 AM
Just thought I would mention something. The use of the short form <?=$variable ?> is considered bad practice these days and in fact short tags <? and ?> instead of <?php and ?> are seriously discouraged due to conflicts with XML. I would recommend that you use <?php echo $variable ?> rather than that short form. Its only a few extra characters and makes your code conform to best practices.

I agree with this. As the short form will work only if it is allowed in the server otherwise it'll fail. So it is always better to code based on the standards even if the length of the code is bit more.

kuau
07-21-2008, 09:05 AM
Now how did you guys know that I was just today deliberating about that very question? I find the long tags very distracting when I am trying to follow the code, so I took them all out of this one file this afternoon just so I could think. But I'll put them all back again, I promise. Thanks! e :)

GarethMc
07-21-2008, 09:15 AM
Now how did you guys know that I was just today deliberating about that very question? I find the long tags very distracting when I am trying to follow the code, so I took them all out of this one file this afternoon just so I could think. But I'll put them all back again, I promise. Thanks! e :)

Actually the long tags make it clearer because it is without a doubt NOT XML when someone else tries to read your code. And if they really do bother you that badly seeing them there then whitespace to make it clearer if you want, PHP doesn't care if you have 20 tabs and 17 spaces and 15 newlines between <?php and echo $variable....

kuau
07-21-2008, 09:29 AM
I'm just not used to them is all. Do you happen to know how to do this?...

LEFT JOIN table1 ON (table1_id = table2_id)

This is a one-to-many relationship with table2 having multiple categories that apply to each id in Table1. How do you display the categories in a list like category1, category2, category3 when you don't know in advance how many there are? And how does the LEFT JOIN deliver them? I figure it is maybe a while loop or a foreach (?). I think I know how to do it with a separate select statement, but can't imagine this way. Thanks, e :)

GarethMc
07-21-2008, 09:50 AM
Ok, you might need to explain better using a short example of your own table to explain what you want as I am not 100% sure what you mean....

That SQL you printed:


LEFT JOIN table1 ON (table1_id = table2_id)

basically makes each record in table 1 add all the extra fields from table 2 to it that you specify after your SELECT where the table1_id matches the table 2_id... And if there are multiple matches (as you said one-to-many) it will duplicate the table 1 records where there are multiple matches just add the difference from table 2 onto it...

codeexploiter
07-21-2008, 09:50 AM
The LEFT JOIN will return all the records from the first table in the SELECT statement, even if the second table lack any matching records for any of the first table's record(s).

Example


SELECT Employees.Name, Dept.DeptName
FROM Employees
LEFT JOIN Dept
ON Employees.deptID=Dept.deptID

In the above mentioned example it will display all the employee names even without a deptName associated.

GarethMc
07-21-2008, 09:57 AM
The LEFT JOIN will return all the records from the first table in the SELECT statement, even if the second table lack any matching records for any of the first table's record(s).

Example


SELECT Employees.Name, Dept.DeptName
FROM Employees
LEFT JOIN Dept
ON Employees.deptID=Dept.deptID

In the above mentioned example it will display all the employee names even without a deptName associated.

True yes, and it will also duplicate the employee names if they have multiple departments they are members of...

kuau
07-21-2008, 10:20 AM
All I'm trying to do is get the categories that apply to a specific event and display them on the screen. Maybe I should just use separate select statements. But I thought JOINs were supposed to make more efficient use of the server resources so I thought I would try one.

I'm trying to keep things simple but it didn't work the way I thought it would. Thanks, e :)

codeexploiter
07-21-2008, 10:36 AM
Can you share the structure of the table you are dealing with so that we'll be able to have a more grip on it rather than giving our assumptions. Sometimes you can achieve those without explicitly using JOIN statements in the SQL.

kuau
07-21-2008, 11:05 AM
It's very simple but I have been floundering around. I think it is probably like the one above.

event table has an event_id selected
eventcat has two fields event_id and cat_id
category table has 2 fields cat_id and category

Need to display the category names for the event_id

I'm really tired and have to get this finished before I go to bed. Can't see straight anymore. Thanks, e :)

codeexploiter
07-21-2008, 11:22 AM
Try the following code


SELECT eventcat.eventID, category.catName
FROM eventcat, category
WHERE eventcat.catid = category.catid

kuau
07-21-2008, 11:34 AM
Dear Codex: That was brilliant! It worked first time. I had a page of code with 2 loops. I swear I am a moron. Thank you SOOOO much! e :)

kuau
07-21-2008, 12:28 PM
Oh, I forgot to find out how to display these.

I only know how to do this:

$category = $row3['category'];

and display like this:

<p>Categories: <?php echo $categories; ?></p>

but there are varying numbers for different events, and maybe none. Just one more hint please. Thx, e :)

kuau
07-21-2008, 09:39 PM
Got this fixed, thanks. If anyone needs to do the same thing, here is the brilliant solution supplied by theTestingSite:


$categories = Array();

while ($row3 = mysql_fetch_array($result3)) {
$categories[] = $row3['category'];
}