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
                // Connect to the database server
                $dbcnx = @mysql_connect('localhost', 'root', 'password');
                if (!$dbcnx) {
                    exit('<p>Unable to connect to the database server at this time.</p>');
                }

                // Select the database
                if (!@mysql_select_db('customerdb')) {
                    exit('<p>Unable to locate the database at this time');
                }

                ?>

<p>Here are all the customers:</p><br />

// Request data from the database
                $customers = @mysql_query('SELECT * FROM customers');
                if (!$customers) {
                    exit('<p>Error performing query: ' . mysql_error() . '</p>');
                }

            while ($row = mysql_fetch_array($customers)) {
                    $customerID = $row['kundeID'];
                    $customer_forename = $row['forename'];
                    $customer_surname = $row['surname'];
                    echo '<p>' . $customer_forename .
                    $customer_surname . 
                    ' <a href="' . $_SERVER['PHP_SELF'] .
                    '?showcustomer=' . $customerID . '">' .
                    'customer details</a></p>';
                } 

                ?>

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!

Recommended Answers

All 6 Replies

Hi there,

To get a space you could the html

&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

You can do that above, but simply adding a quoted space should suffice as well:

echo '<p>' . $customer_forename . " " . $customer_surname .

Never use a hammer when a screwdriver will do.

Ok guys, thanks for the help so far! :D
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!

Also there is function called die() that will save you alot of coding.You have:

#
$dbcnx = @mysql_connect('localhost', 'root', 'password');
#
if (!$dbcnx) {
#
exit('<p>Unable to connect to the database server at this time.</p>');
#
}

but you can do this:

$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:


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? :)

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

if(isset($_REQUEST['showcustomer'])){
   $query = 'SELECT * FROM customers where customerId = '.$_REQUEST['showcustomer'];
    $customerDetails = @mysql_query($query); //here is the data u need. Use it the way u want
}

Hope this helps

Ok guys, thanks for the help so far! :D
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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.