Newbie in need of help with some PHP code :)

Reply

Join Date: Nov 2009
Posts: 2
Reputation: hawkontvn is an unknown quantity at this point 
Solved Threads: 0
hawkontvn hawkontvn is offline Offline
Newbie Poster

Newbie in need of help with some PHP code :)

 
0
  #1
Nov 5th, 2009
Hey! I'm quite new to this whole thing, so please don't fire me with shait on this one =D

I'm trying to learn PHP and MySQL, and atm I am trying to make a website which corresponds with a database - a database with customers.

1. I want the page to show all the customers in the database - with first- and surname.

2. I want each customer to be clickable - when you click a customer, it shows all the information about that customer which is stored in the database.

I can post some of the stuff I tried out with, but I'm not good with PHP (yet! =D), and therefore I got stuck quite early... but here's some of the stuff I've done so far..

  1. <?php
  2. // Connect to the database server
  3. $dbcnx = @mysql_connect('localhost', 'root', 'password');
  4. if (!$dbcnx) {
  5. exit('<p>Unable to connect to the database server at this time.</p>');
  6. }
  7.  
  8. // Select the database
  9. if (!@mysql_select_db('customerdb')) {
  10. exit('<p>Unable to locate the database at this time');
  11. }
  12.  
  13. ?>
  14.  
  15. <p>Here are all the customers:</p><br />
  16.  
  17. // Request data from the database
  18. $customers = @mysql_query('SELECT * FROM customers');
  19. if (!$customers) {
  20. exit('<p>Error performing query: ' . mysql_error() . '</p>');
  21. }
  22.  
  23. while ($row = mysql_fetch_array($customers)) {
  24. $customerID = $row['kundeID'];
  25. $customer_forename = $row['forename'];
  26. $customer_surname = $row['surname'];
  27. echo '<p>' . $customer_forename .
  28. $customer_surname .
  29. ' <a href="' . $_SERVER['PHP_SELF'] .
  30. '?showcustomer=' . $customerID . '">' .
  31. 'customer details</a></p>';
  32. }
  33.  
  34. ?>

This piece of code also gives me no spaces between the $customer_forename and $customer_surname.. How can I make a space between them?

And one last question In this example, I have just created a link at the end of the customers listed - which says customer details.. Is there a way to make the $customer_forename and $customer_surname a clickable link which executes what I am trying to achieve?

Thanks, and sorry for not being really good at this, at all! =D
Have a nice day!
Last edited by hawkontvn; Nov 5th, 2009 at 2:33 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 49
Reputation: emhmk1 is an unknown quantity at this point 
Solved Threads: 0
emhmk1 emhmk1 is offline Offline
Light Poster
 
0
  #2
Nov 5th, 2009
Hi there,

To get a space you could the html
  1. &nbsp
That should sort that, as for making the name clickable replace 'customer details' in the link to .$customer_forname. "&nbsp" .$customer_surname.

That should work in theory

Hope it helps
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 26
Reputation: Cronless is an unknown quantity at this point 
Solved Threads: 2
Cronless Cronless is offline Offline
Light Poster
 
1
  #3
Nov 5th, 2009
You can do that above, but simply adding a quoted space should suffice as well:
  1. echo '<p>' . $customer_forename . " " . $customer_surname .
Never use a hammer when a screwdriver will do.
~~ Free Cron Jobs - Online Cron Service ~~
Best place to run your PHP Cron Jobs
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 2
Reputation: hawkontvn is an unknown quantity at this point 
Solved Threads: 0
hawkontvn hawkontvn is offline Offline
Newbie Poster

Thanks so far :)

 
0
  #4
Nov 5th, 2009
Ok guys, thanks for the help so far!
I should have known the &nbsp or the empty " " would work for the spacing... I also managed to make the names clickable..

But I still lack the tricky stuff :p

I made the names from the database clickable, but how can I make them show the rest of the data associated with the names show when I click a name?

Hope somebody can help out!
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,492
Reputation: evstevemd has a spectacular aura about evstevemd has a spectacular aura about evstevemd has a spectacular aura about 
Solved Threads: 128
evstevemd's Avatar
evstevemd evstevemd is offline Offline
Nearly a Posting Virtuoso
 
0
  #5
Nov 5th, 2009
Also there is function called die() that will save you alot of coding.You have:
  1. #
  2. $dbcnx = @mysql_connect('localhost', 'root', 'password');
  3. #
  4. if (!$dbcnx) {
  5. #
  6. exit('<p>Unable to connect to the database server at this time.</p>');
  7. #
  8. }

but you can do this:
  1. $dbcnx = @mysql_connect('localhost', 'root', 'password') or die('<p>Unable to connect to the database server at this time.</p>');
See how simple it is :lol:
Atheist: God is man made imagination, he doesn't exist!
Theist: It's okay, can you imagine anything else that doesn't exist?
---- Python, C++ PHP and Java ----
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,492
Reputation: evstevemd has a spectacular aura about evstevemd has a spectacular aura about evstevemd has a spectacular aura about 
Solved Threads: 128
evstevemd's Avatar
evstevemd evstevemd is offline Offline
Nearly a Posting Virtuoso
 
0
  #6
Nov 5th, 2009
Originally Posted by hawkontvn View Post

I made the names from the database clickable, but how can I make them show the rest of the data associated with the names show when I click a name?
I don't get you here! What do you mean? display data from database or what?
Atheist: God is man made imagination, he doesn't exist!
Theist: It's okay, can you imagine anything else that doesn't exist?
---- Python, C++ PHP and Java ----
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 185
Reputation: venkat0904 is an unknown quantity at this point 
Solved Threads: 19
venkat0904's Avatar
venkat0904 venkat0904 is offline Offline
Junior Poster
 
0
  #7
Nov 6th, 2009
ur link points to php self ie the same file. I suggest u to handle customer details retrieval from another php file.
anyway if u want to do that from the same file itself, u can have an if statement like
  1. if(isset($_REQUEST['showcustomer'])){
  2. $query = 'SELECT * FROM customers where customerId = '.$_REQUEST['showcustomer'];
  3. $customerDetails = @mysql_query($query); //here is the data u need. Use it the way u want
  4. }

Hope this helps


Originally Posted by hawkontvn View Post
Ok guys, thanks for the help so far!
I should have known the &nbsp or the empty " " would work for the spacing... I also managed to make the names clickable..

But I still lack the tricky stuff :p

I made the names from the database clickable, but how can I make them show the rest of the data associated with the names show when I click a name?

Hope somebody can help out!
Last edited by venkat0904; Nov 6th, 2009 at 2:15 am.
Reply With Quote Quick reply to this message  
Reply

Message:




Views: 258 | Replies: 6
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC