 |
|
|
04-09-2005, 04:03 PM
|
Displaying ABC
|
Posts: 31
Location: UK
|
I need this page to dispay the names begining with the letter they select, can any one help?
The Page -
ABCDEFGHIJKLMNOPQRSTUVWXYZ
The Code -
<?php
$sql = "SELECT * FROM adddb ORDER BY nameof";
$result = mysql_query($sql, $conn) or die(mysql_error());
while ($newArray = mysql_fetch_array($result)) {
$nameof = $newArray['nameof'];
$why = $newArray['why'];
echo "
<b>Name:</b><br>$nameof<br><br><b>Because:</b><br>$why<br><br><hr>";
}
?>
Thanx
Jamie
__________________
Please login or register to view this content. Registration is FREE
- Hosting From £9.99PY 500MB Space - 10GB Bandwidth - Domains From £8.00PY
|
|
|
|
04-09-2005, 06:11 PM
|
|
Posts: 297
|
Hmmmm right then.
For a start, lets use a Query string taken from the link pressed on (i'm assuming all the letters are linked, so we'd have something like http://www.mysite.com/names.php?letter=A).
so:
PHP Code:
$LetterPrefix = $_GET['letter'];
Now we can simply use a SQL query to find anything in the database beginning with the letter given to us.
$sql = "SELECT * FROM 'adddb' WHERE 1 AND 'nameof' LIKE '$LetterPrefix%' ORDER BY 'nameof'";
and hey presdo! Should do it, message back if it works or don't work
Sorry forgot to explain something, i added a WHERE LIKE command, it's pretty simple and can be investigated further on the official SQL site. But the % you see after $LetterPrefix is a wildcard, so basically it's going to search for any name in the database starting with that letter, and does not concern itself with anything after that
__________________
A lie gets halfway around the world before the truth has a chance to get its pants on. - Sir Winston Churchill
Please visit my sites: Please login or register to view this content. Registration is FREE | Please login or register to view this content. Registration is FREE
Last edited by leavethisplace; 04-09-2005 at 06:13 PM..
Reason: needed to explain something
|
|
|
|
04-09-2005, 06:48 PM
|
|
Posts: 31
Location: UK
|
Quote:
|
Originally Posted by leavethisplace
|
How would i do that bit?
Sorry
Jamie
__________________
Please login or register to view this content. Registration is FREE
- Hosting From £9.99PY 500MB Space - 10GB Bandwidth - Domains From £8.00PY
|
|
|
|
04-09-2005, 08:39 PM
|
|
Posts: 1,832
Location: Somewhere else entirely
|
For the letter index, write your links with the letters on the end, as 'get' variables:
HTML Code:
<a href="http://www.mysite.com/names.php?letter=A">A</a>
<a href="http://www.mysite.com/names.php?letter=B">B</a>
<a href="http://www.mysite.com/names.php?letter=C">C</a>
...
<a href="http://www.mysite.com/names.php?letter=Z">Z</a>
Then in names.php or whatever filename you point the links at, you can determine which letter was clicked on by looking in the $_GET array that PHP provides you:
PHP Code:
<?php
$clickedletter = $_GET['letter'];
//'letter' is the same name as you used for the variable in the links
echo "You clicked on letter ". $clickedletter;
?>
That make sense?
You can then use the knowledge of what letter was clicked to run a MySQL query to find only the things that start with that letter:
PHP Code:
$result = mysql_query("SELECT * FROM adddb WHERE nameof LIKE '$clickedletter%'");
The % sign indicates '0 or more characters, so $clickedletter% will match up with anything in nameof that starts with the clicked letter.
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';
Please login or register to view this content. Registration is FREE (aka MSN handwriting for forums)
Last edited by 0beron; 04-09-2005 at 08:47 PM..
|
|
|
|
04-09-2005, 09:00 PM
|
|
Posts: 31
Location: UK
|
PHP Code:
<?php include("includes/dbconnect.php"); ?>
<a href="http://www.site.com/index.php?letter=A">A</a>
<a href="http://www.site.com/index.php?letter=B">B</a>
<a href="http://www.site.com/index.php?letter=C">C</a>
<?php
$firstLetter = $_GET['letter'];
$sql = "SELECT * FROM adddb WHERE " .$nameof. " LIKE " .$firstLetter. " ORDER BY 'nameof'";
$result = mysql_query($sql, $conn) or die(mysql_error());
while ($row = mysql_fetch_assoc($result)) {
$name = $row["nameof"];
$why = $row['why'];
echo "<b>Name:</b><br>" .$name. "<br><br>";
echo "<b>Because:</b><br>" .$why. "<br><br><hr>";
}
?>
still stuck, any ideas, thats what i have got now?
Thanx for your help guys
__________________
Please login or register to view this content. Registration is FREE
- Hosting From £9.99PY 500MB Space - 10GB Bandwidth - Domains From £8.00PY
Last edited by simjay; 04-09-2005 at 09:19 PM..
|
|
|
|
04-10-2005, 04:26 AM
|
|
Posts: 297
|
PHP Code:
$sql = "SELECT * FROM adddb WHERE " .$nameof. " LIKE " .$firstLetter. " ORDER BY 'nameof'";
There is no need for the " . $value . " thing here man, take that out, SQL statement will work even if you just have the variable name without " . . "
So
PHP Code:
$sql = "SELECT * FROM 'adddb' WHERE 1 AND 'nameof' LIKE '$LetterPrefix%' ORDER BY 'nameof'";
If that don't work try adding values in manually to check that the SQL statement is working correctly, so try something like
PHP Code:
$sql = "SELECT * FROM 'adddb' WHERE 1 AND 'nameof' LIKE 'A%' ORDER BY 'nameof'";
Also, do a if ($isset($_GET[letter])) { statement to check that the letter is present in the URL.
Also, whats with copying exactly what I said Oberon??
__________________
A lie gets halfway around the world before the truth has a chance to get its pants on. - Sir Winston Churchill
Please visit my sites: Please login or register to view this content. Registration is FREE | Please login or register to view this content. Registration is FREE
|
|
|
|
04-10-2005, 06:07 AM
|
|
Posts: 1,832
Location: Somewhere else entirely
|
Oh sorry - I didn't fully read your post where you explained the SQL - i just saw the request to clarify what a query string was, then I went on to the other parts of the code. Didn't realise you'd already done it.
What exactly do you need the WHERE 1 for?
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';
Please login or register to view this content. Registration is FREE (aka MSN handwriting for forums)
|
|
|
|
04-10-2005, 01:21 PM
|
|
Posts: 297
|
errr... cant actually explain why i put that there.. ignore it! It's really just a trick of the eeeeyyyeeee 
__________________
A lie gets halfway around the world before the truth has a chance to get its pants on. - Sir Winston Churchill
Please visit my sites: Please login or register to view this content. Registration is FREE | Please login or register to view this content. Registration is FREE
|
|
|
|
04-10-2005, 01:53 PM
|
|
Posts: 31
Location: UK
|
ahhhhhhhhhhhh this does not want to work lol
__________________
Please login or register to view this content. Registration is FREE
- Hosting From £9.99PY 500MB Space - 10GB Bandwidth - Domains From £8.00PY
|
|
|
|
04-10-2005, 05:58 PM
|
|
Posts: 297
|
Ok then dude, copy the code out below EXACTLY how it looks, dont change one thing, not even a little bit, don't even take one tiny bit oh whitespace out! Copy and paste it, and try it out, it should work, if not it should throw up an error!
PHP Code:
<?php include("includes/dbconnect.php"); ?>
<a href="http://www.site.com/index.php?letter=A">A</a>
<a href="http://www.site.com/index.php?letter=B">B</a>
<a href="http://www.site.com/index.php?letter=C">C</a>
<?php
if (isset($_GET['letter']))
{
$firstLetter = $_GET['letter'];
$sql_query = "SELECT * FROM adddb WHERE '$nameof' LIKE '$firstLetter%' ORDER BY 'nameof'";
$result = mysql_query($sql_query)
or die("Query failed: " . mysql_error());
while ($row = mysql_fetch_assoc($result)) {
$name = $row['nameof'];
$why = $row['why'];
if ((!isset($name)) || (!isset($why))) {
die("PHP Error: Variable 'name' or 'why' not set");
} else {
echo('<b>Name:</b><br>' . $name . '<br><br>
<b>Because:</b><br>' . $why . '<br><br><hr>');
}
}
?>
__________________
A lie gets halfway around the world before the truth has a chance to get its pants on. - Sir Winston Churchill
Please visit my sites: Please login or register to view this content. Registration is FREE | Please login or register to view this content. Registration is FREE
|
|
|
|
04-11-2005, 11:36 AM
|
Closer
|
Posts: 31
Location: UK
|
that didnt work so i tried this and now im alot closer-
abc.php
PHP Code:
<?php
$link = "list.php?alphabet";
echo "<a href='$link=a'>A</a> | <a href='$link=b'>B</a> | " ;
?>
list.php
PHP Code:
<?php
$sql = 'SELECT * from adddb where nameof = "alphabet"';
$result = mysql_query($sql, $conn) or die(mysql_error());
while ($newArray = mysql_fetch_array($result)) {
$nameof = $newArray['nameof'];
$why = $newArray['why'];
echo "
<b>Name:</b><br>
$nameof<br><br>
<b>Why:</b><br>
$why<br><hr>";
}
?>
Any ideas?
__________________
Please login or register to view this content. Registration is FREE
- Hosting From £9.99PY 500MB Space - 10GB Bandwidth - Domains From £8.00PY
|
|
|
|
04-11-2005, 12:11 PM
|
|
Posts: 297
|
Why are u using $conn in mysql_query($sql, $conn) whats it for?
__________________
A lie gets halfway around the world before the truth has a chance to get its pants on. - Sir Winston Churchill
Please visit my sites: Please login or register to view this content. Registration is FREE | Please login or register to view this content. Registration is FREE
|
|
|
|
04-11-2005, 12:25 PM
|
|
Posts: 31
Location: UK
|
i was using it b4 to see if i could find the error, but didnt help.
__________________
Please login or register to view this content. Registration is FREE
- Hosting From £9.99PY 500MB Space - 10GB Bandwidth - Domains From £8.00PY
|
|
|
|
04-11-2005, 12:31 PM
|
|
Posts: 297
|
Ok, i dont think it's needed. I know the SQL is correct because it works on my own scripts.
What exactly happens? Does it return anything? Also, dump your SQL data and structure here, maybe the problem is fair deep.
__________________
A lie gets halfway around the world before the truth has a chance to get its pants on. - Sir Winston Churchill
Please visit my sites: Please login or register to view this content. Registration is FREE | Please login or register to view this content. Registration is FREE
|
|
|
|
04-11-2005, 12:36 PM
|
|
Posts: 31
Location: UK
|
all i need is the bit so it only looks up the first letter of name $nameof
__________________
Please login or register to view this content. Registration is FREE
- Hosting From £9.99PY 500MB Space - 10GB Bandwidth - Domains From £8.00PY
|
|
|
|
04-11-2005, 12:49 PM
|
|
Posts: 1,832
Location: Somewhere else entirely
|
that's what 'LIKE' is for - look back at the previous posts, it's been explained twice already (I didn't see the first time!)
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';
Please login or register to view this content. Registration is FREE (aka MSN handwriting for forums)
|
|
|
|
04-11-2005, 12:51 PM
|
|
Posts: 297
|
oh right, can see the error in your coding now, even though i've corrected it loads. The mySQL statement is incorrect, musta copied it wrong or something
PHP Code:
$sql = "SELECT * from adddb where nameof LIKE '$alphabet%'";
You must use %, it's a wildcard. You must also use LIKE, cause i said so
Also, make sure you wrap the variable value in ""
Try that
__________________
A lie gets halfway around the world before the truth has a chance to get its pants on. - Sir Winston Churchill
Please visit my sites: Please login or register to view this content. Registration is FREE | Please login or register to view this content. Registration is FREE
|
|
|
|
04-11-2005, 01:18 PM
|
|
Posts: 31
Location: UK
|
Hurray it works, lol
Thanx alot guys for all your help
Thank You Thank You Thank You Thank You
__________________
Please login or register to view this content. Registration is FREE
- Hosting From £9.99PY 500MB Space - 10GB Bandwidth - Domains From £8.00PY
|
|
|
|
04-11-2005, 01:56 PM
|
|
Posts: 297
|
well actually i was expecting a big kiss too, but thank you is enough
It's cool dude, good luck with the rest of your coding!
__________________
A lie gets halfway around the world before the truth has a chance to get its pants on. - Sir Winston Churchill
Please visit my sites: Please login or register to view this content. Registration is FREE | Please login or register to view this content. Registration is FREE
|
|
|
|
|
« Reply to Displaying ABC
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|