944,117 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 434
  • PHP RSS
Nov 5th, 2009
0

Newbie in need of help with some PHP code :)

Expand Post »
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..

PHP Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hawkontvn is offline Offline
2 posts
since Nov 2009
Nov 5th, 2009
0
Re: Newbie in need of help with some PHP code :)
Hi there,

To get a space you could the html
PHP Syntax (Toggle Plain Text)
  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
Reputation Points: 11
Solved Threads: 7
Junior Poster
emhmk1 is offline Offline
107 posts
since Feb 2008
Nov 5th, 2009
1
Re: Newbie in need of help with some PHP code :)
You can do that above, but simply adding a quoted space should suffice as well:
PHP Syntax (Toggle Plain Text)
  1. echo '<p>' . $customer_forename . " " . $customer_surname .
Never use a hammer when a screwdriver will do.
Reputation Points: 14
Solved Threads: 2
Light Poster
Cronless is offline Offline
29 posts
since Sep 2009
Nov 5th, 2009
0

Thanks so far :)

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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hawkontvn is offline Offline
2 posts
since Nov 2009
Nov 5th, 2009
0
Re: Newbie in need of help with some PHP code :)
Also there is function called die() that will save you alot of coding.You have:
php Syntax (Toggle Plain Text)
  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:
php Syntax (Toggle Plain Text)
  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:
Reputation Points: 462
Solved Threads: 392
Senior Poster
evstevemd is offline Offline
3,681 posts
since Jun 2007
Nov 5th, 2009
0
Re: Newbie in need of help with some PHP code :)
Click to Expand / Collapse  Quote originally posted by hawkontvn ...

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?
Reputation Points: 462
Solved Threads: 392
Senior Poster
evstevemd is offline Offline
3,681 posts
since Jun 2007
Nov 6th, 2009
0
Re: Newbie in need of help with some PHP code :)
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
PHP Syntax (Toggle Plain Text)
  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


Click to Expand / Collapse  Quote originally posted by hawkontvn ...
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.
Reputation Points: 13
Solved Threads: 21
Junior Poster
venkat0904 is offline Offline
186 posts
since Oct 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: mail() problem
Next Thread in PHP Forum Timeline: SMS gateway in php





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC