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 11-05-2004, 06:50 AM conversion of values
pop
Junior Talker

Posts: 4
Trades: 0
Hi,

I have the following 2 codes :

include "connectdb.php";

$num_results = 5;
$result = mysql_query("select diary.*, users.name from diary, users where diary.poster = users.username order by postno desc limit $num_results");

if(!$result) echo "Database error!";
if($num_results > mysql_num_rows($result))
$num_results = mysql_num_rows($result);

for($i=0;$i<$num_results;$i++) {
$rows = mysql_fetch_array($result);
echo "<strong>".$rows["name"].">>".$rows["datetime"]."</strong><P ALIGN=JUSTIFY>
".$rows["text"]."</p>";


What this code does is to convert the value 'poster' into 'name', where poster is a user's login and 'name' is the user's real name. This code will produce an output like :

Mark Lee>>Nov 4,12:33 pm 2004

Where Mark Lee is the name.

--

I have another code :


include "connectdb.php";

$result = mysql_query("select * from diary where datetime like '$month%' and datetime like '%$year' order by postno desc");

if(!$result) echo "Database error!";
$num_results = mysql_num_rows($result);

for($i=0;$i<$num_results;$i++) {
$rows = mysql_fetch_array($result);
echo "<strong>".$rows["poster"].">>".$rows["datetime"]."</strong><P ALIGN=JUSTIFY>
".$rows["text"]."</p>";


As you can see from the code, this code will only produce the value of poster, ie the user login name and NOT the user's real name.

I would like to change this but I am not sure how to. I cannot change 'poster' to 'name' for the code doesn't know what is 'name' as it hasnt been coded beforehand, unlike the first source code. Could someone please give a tip or two on how to instruct the second code to convert 'poster' into 'name' like the first code please?

thank you!
pop is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 11-05-2004, 08:57 AM
Kyrnt's Avatar
The Post-Mod Years

Posts: 2,536
Location: Western Maryland
Trades: 0
Try this SQL.

Code:
SELECT d.*, u.name FROM diary d INNER JOIN users u ON d.post=u.username WHERE d.datetime like '$month%' and d.datetime like '%$year' order by d.postno desc
__________________
—Kyrnt
Kyrnt is offline
Reply With Quote
View Public Profile Visit Kyrnt's homepage!
 
Old 11-05-2004, 09:03 AM
pop
Junior Talker

Posts: 4
Trades: 0
it didn't work...
pop is offline
Reply With Quote
View Public Profile
 
Old 11-05-2004, 09:08 AM
Kyrnt's Avatar
The Post-Mod Years

Posts: 2,536
Location: Western Maryland
Trades: 0
I think I made an error in the SQL (post instead of poster).

Code:
SELECT d.*, u.name FROM diary d INNER JOIN users u ON d.poster=u.username WHERE d.datetime like '$month%' and d.datetime like '%$year' order by d.postno desc
Ok, just so I understand your schema, let me lay out my assumptions and you tell me if they are correct.

users.name is the user's real name.
diary.poster and users.username should be equal in determining which entries in the diary table belong to a particular user.

If these are true, then the JOIN is valid, but as I said, I made an error in the SQL that has now been corrected.

One suggestion, assign primary keys to each of your tables and use those to perform cross-table correlations. It is a much more efficient process to compare integers than it is to compare strings.
__________________
—Kyrnt
Kyrnt is offline
Reply With Quote
View Public Profile Visit Kyrnt's homepage!
 
Old 11-05-2004, 09:15 AM
pop
Junior Talker

Posts: 4
Trades: 0
hmm, yes user.name is user's real name. poster is like a nick or something. no they dont have to be equal.

the poster and the name fields are in seperate tables. that makes it a little more complicated.

and YES, i did the above changes, and i changed

echo "<strong>".$rows["poster"].">>".$rows

from poster to name, and it solved the problem!

thank you!

now i have to carefully examine your code..i will take ur suggestion. in fact i think i ought to have the same keys for each table. whats the point of having a 'name' in the user table, and a 'poster' in the other table..hmm
pop is offline
Reply With Quote
View Public Profile
 
Old 11-05-2004, 09:34 AM
Kyrnt's Avatar
The Post-Mod Years

Posts: 2,536
Location: Western Maryland
Trades: 0
Agreed. It seems that you may be able to combine some of the fields in the two tables into the columns of the users table.

I'm glad we got your problem solved -- good luck.
__________________
—Kyrnt
Kyrnt is offline
Reply With Quote
View Public Profile Visit Kyrnt's homepage!
 
Old 11-05-2004, 09:36 AM
pop
Junior Talker

Posts: 4
Trades: 0
Quote:
Originally Posted by Kyrnt
Agreed. It seems that you may be able to combine some of the fields in the two tables into the columns of the users table.

I'm glad we got your problem solved -- good luck.
it was built for this blogging thing and i cant change the keys without screwing the whole thing. thats bad programming i guess, not planning in advanced.

but yeh will take note, and thanks for your tip. appreciate it lots!
pop is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to conversion of values
 

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 1.14017 seconds with 12 queries