Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 04-09-2005, 04:03 PM Displaying ABC
simjay's Avatar
Experienced Talker

Posts: 31
Location: UK
Trades: 0
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
simjay is offline
Reply With Quote
View Public Profile Visit simjay's homepage!
 
 
Register now for full access!
Old 04-09-2005, 06:11 PM
leavethisplace's Avatar
Ultra Talker

Posts: 297
Trades: 0
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
leavethisplace is offline
Reply With Quote
View Public Profile
 
Old 04-09-2005, 06:48 PM
simjay's Avatar
Experienced Talker

Posts: 31
Location: UK
Trades: 0
Quote:
Originally Posted by leavethisplace
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).
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
simjay is offline
Reply With Quote
View Public Profile Visit simjay's homepage!
 
Old 04-09-2005, 08:39 PM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
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..
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 04-09-2005, 09:00 PM
simjay's Avatar
Experienced Talker

Posts: 31
Location: UK
Trades: 0
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..
simjay is offline
Reply With Quote
View Public Profile Visit simjay's homepage!
 
Old 04-10-2005, 04:26 AM
leavethisplace's Avatar
Ultra Talker

Posts: 297
Trades: 0
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
leavethisplace is offline
Reply With Quote
View Public Profile
 
Old 04-10-2005, 06:07 AM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
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)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 04-10-2005, 01:21 PM
leavethisplace's Avatar
Ultra Talker

Posts: 297
Trades: 0
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
leavethisplace is offline
Reply With Quote
View Public Profile
 
Old 04-10-2005, 01:53 PM
simjay's Avatar
Experienced Talker

Posts: 31
Location: UK
Trades: 0
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
simjay is offline
Reply With Quote
View Public Profile Visit simjay's homepage!
 
Old 04-10-2005, 05:58 PM
leavethisplace's Avatar
Ultra Talker

Posts: 297
Trades: 0
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
leavethisplace is offline
Reply With Quote
View Public Profile
 
Old 04-11-2005, 11:36 AM Closer
simjay's Avatar
Experienced Talker

Posts: 31
Location: UK
Trades: 0
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
simjay is offline
Reply With Quote
View Public Profile Visit simjay's homepage!
 
Old 04-11-2005, 12:11 PM
leavethisplace's Avatar
Ultra Talker

Posts: 297
Trades: 0
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
leavethisplace is offline
Reply With Quote
View Public Profile
 
Old 04-11-2005, 12:25 PM
simjay's Avatar
Experienced Talker

Posts: 31
Location: UK
Trades: 0
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
simjay is offline
Reply With Quote
View Public Profile Visit simjay's homepage!
 
Old 04-11-2005, 12:31 PM
leavethisplace's Avatar
Ultra Talker

Posts: 297
Trades: 0
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
leavethisplace is offline
Reply With Quote
View Public Profile
 
Old 04-11-2005, 12:36 PM
simjay's Avatar
Experienced Talker

Posts: 31
Location: UK
Trades: 0
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
simjay is offline
Reply With Quote
View Public Profile Visit simjay's homepage!
 
Old 04-11-2005, 12:49 PM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
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)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 04-11-2005, 12:51 PM
leavethisplace's Avatar
Ultra Talker

Posts: 297
Trades: 0
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
leavethisplace is offline
Reply With Quote
View Public Profile
 
Old 04-11-2005, 01:18 PM
simjay's Avatar
Experienced Talker

Posts: 31
Location: UK
Trades: 0
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
simjay is offline
Reply With Quote
View Public Profile Visit simjay's homepage!
 
Old 04-11-2005, 01:56 PM
leavethisplace's Avatar
Ultra Talker

Posts: 297
Trades: 0
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
leavethisplace is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Displaying ABC
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.58375 seconds with 12 queries