Jump to content

Need help with some php code :)


hawkontvn

Recommended Posts

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.

 

I managed to make the page show all the customers in the database - with first- and surname.

 

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

 

So far I have managed to make them clickable, but I don't know how to make it so that the click posts all the associated information about that customer on the page..

 

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>';
                } 

                ?>

 

Thanks! :D

Link to comment
https://forums.phpfreaks.com/topic/180495-need-help-with-some-php-code/
Share on other sites

Yea that's the right track, so when somebody clicks on the customer details for a customer they are taken to the URL

 

myfile.php?showcustomer=15

 

Where 15 is the customer ID.

 

Then on my file.php you just get this value and look for it in the database like:

 

<?php
if($_GET['showcustomer']){
//Then Somebody is on a Customer Details Page

//Get the Customer ID Value from the URL
$custID = $_GET['showcustomer'];

echo "Hello, Here are Customer Details for the customer with ID: $custID <br />";

//Search For Customer Information Based on the ID Given
$query= "SELECT FROM tablename WHERE custID = $custID";
$res = mysql_query($query) or die(mysql_error());

//Now Loop Through the Result and Display The Information found about the ID
while(mysql_fetch_array($res) = row){
	//echo out customer details here
}
}

?>

 

You'd replace tablename with table name and custID with the field name that stores the customer ID field.

 

Thanks for the help :)

 

I managed it all pretty well, though I think my code got a lil' bit more messy than it could have been...

 

anyways, I have one last problem..

I tried echoing the customer details out into a table.. i managed the table itself, but somehow it fucked up the css or at least look of the page..

my page is a 3-column-awebsite.. It is divided into three <div>'s, like this:

 

<div id="left"> - left column

</div>

<div id="right"> - right column

<div id="right2"> - a col inside the right one, making up the middle col.

</div> (end of right2 div)

</div> (end of right div)

 

So, when I echoed out the table with my data into the right column (div id="right"), it somehow overlapped into the right2 div, and fucked up the whole page :D

 

any ideas? thanks! :)

Nah, that won't help it - already tried it out..think its because the minimum size of the table (because of the content) is too big - even though its not such a big table.. the funny thing is i tried to make the columns bigger so that the table would fit, but still no luck..

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.