Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,152,827 members, 7,817,413 topics. Date: Saturday, 04 May 2024 at 11:50 AM

PHP Masters Pls Help A PHP Learner Please. - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / PHP Masters Pls Help A PHP Learner Please. (807 Views)

How To Program A PHP SMS Gateway / What Is The Average Salary Of A Php Programmer In Lagos / I Need Help With A PHP Script (2) (3) (4)

(1) (Reply) (Go Down)

PHP Masters Pls Help A PHP Learner Please. by Nobody: 4:35pm On Jul 26, 2013
Please can you tell me how to go abt it? or what am doing wrong.

I am working on a project (shopping cart) that basically add items to basket, user can remove item, it also gives the order summary on the check out page, stores the user billing address and shipping address, and all the normal stuff that a shopping cart can do....

So I created a function.class.php file that will handle the information that is on database, so that details, pics, stock and product name can be extracted from database and the result be displayed on the home page (the page that contains item to be added to cart).
But I have problem in calling (echoing) the result from the function.
E.g.

function getProductName ($pn)
{
$result = mysql_query ("SELECT * FROM product WHERE ProductID = '$pid'"wink;
$row = mysql_fetch_array ($result);
return $row ['Name'];
}

So what I want to do is echo the result of the above function on a script where I included the function.class.php.

NB: In the above function i wrote a line for mysql error, but the problem is not from database.
How do i go abt this....
Thanks
Re: PHP Masters Pls Help A PHP Learner Please. by Nobody: 4:38pm On Jul 26, 2013
the variable inside the function parenthese is supposed to be $pid i corrected that too
Re: PHP Masters Pls Help A PHP Learner Please. by Nobody: 6:28pm On Jul 26, 2013
Don't really get you, buh, this may work.


function getProductName($pid){
$result = mysql_query("SELECT * FROM product WHERE ProductID='".$pid."'"wink;
$row = mysql_fetch_assoc($result);
return $row['Name'];
}
$productName = getProductName(2);
echo $productName;

1 Like

Re: PHP Masters Pls Help A PHP Learner Please. by CyberG: 5:49am On Jul 27, 2013
Since you posted so little code, I can only point out obvious errors.

1. In this function, your local variable is $pn, but your where clause uses $pid. As far as PHP is concerned $pid is undefined and $result will not contain the correct results. It might help to echo $pid to see if it contains correct data. I would think you meant to use $pn since that is the argument in the function.

2. Do you have a 'Name' column in the associative array $row? Do a var_dump($row) or print_r($row) to see if there is a 'Name' column in the returned array, if not, you know why your return statement is not giving you correct data when you echo it. You should see something like:

Array
(
[0] => ...
[...] => ....
[1] => ...
[...] => ....
etc and Name should be there!
)

3. Finally, to actually be able to see all the data returned from your table, you should loop through the results, so put your $row = ... in a while loop:

while ($row = mysql_fetch_array($result))
{
echo $row["id"] . " ==> " . $row["Name"]; //Substitute your correct column names to see the correct results
}

4. After you are certain you can see the correct data within this function, then you can return it but note that you CANNOT simple echo the array like a simple variable. In your calling method / function, you need to iterate through it like I described here and then you can do whatever you like with it. Echo is so, so basic if you are developing a serious app! In fact with some advanced frontend JS libraries, it is beyond ridiculous to 'echo' as it messes up your testing process! Get a nice logging class and log to a file instead and you can preserve the results from run to run and see what is happening!

5. Other observations: mysql_query is deprecated! Stop propagating old, deprecated code/extensions in your project and the internet! Use mysqli_query or PDO::query() instead! I hope some of these ideas help you!

function getProductName ([size=18pt]$pn[/size])
{
$result = mysql_query ("SELECT * FROM product WHERE ProductID = '[size=18pt]$pid[/size]' " );
$row = mysql_fetch_array ($result);
return $row ['Name'];
}

1 Like

Re: PHP Masters Pls Help A PHP Learner Please. by Nobody: 11:41am On Jul 27, 2013
^^Thanks alot. The variable error has been corrected. But I was able to echo the function using the:

functionName ( ); Syntax.
But the problem I have is that, I would have to input the $pid variable whenever I echo the function using the above syntax.

The main problem/task is as follow:

I created a table on the DB with a row for productID (primary key), price, stock_status, and description. So I wrote a function code that would grab the above details from the database and return the value.

I intended to echo the value returned on another page, (since I wrote all the functions in a separate script) so I

require_once "function.class.php";


in the script where I want to use the function. But where I echo the return value of the function using:
get_product _name ( );
I would have to input the productID myself for the details to print.

So my question is that: Is there a way in which I can echo the value without having to input the ProductId myself? I mean it's supposed to be automated...
Thanks for your help
Re: PHP Masters Pls Help A PHP Learner Please. by Nobody: 12:21pm On Jul 27, 2013
You want ot display the list of all the products or just a random product from the function?

1 Like

Re: PHP Masters Pls Help A PHP Learner Please. by Nobody: 12:32pm On Jul 27, 2013
Judinho59: You want ot display the list of all the products or just a random product from the function?
All the product based on the productID, I would still have to write a search script.

AM A LEARNER, JUST A WEEK INTO THE LEARNING OF THE LANGUAGE. please forgive me if all what I am saying sounds silly.
Thanks
Re: PHP Masters Pls Help A PHP Learner Please. by Nobody: 12:42pm On Jul 27, 2013
^^
This shows the list of products in DB..

$query = mysql_query("SELECT * FROM product ORDER BY productID DESC"wink;
if(mysql_num_rows($query) > 0){ //if there's one or more product shwo list

while($row = mysql_fetch_array($query)){
echo"Product ID: ". $row['productID']."
Product Name: ". $row['Name']."
Description: ". $row['description']."
Price: ". $row['price'];
}
}

1 Like

Re: PHP Masters Pls Help A PHP Learner Please. by Nobody: 12:54pm On Jul 27, 2013
Judinho59: ^^
This shows the list of products in DB..

$query = mysql_query("SELECT * FROM product ORDER BY productID DESC"wink;
if(mysql_num_rows($query) > 0){ //if there's one or more product shwo list

while($row = mysql_fetch_array($query)){
echo"Product ID: ". $row['productID']."
Product Name: ". $row['Name']."
Description: ". $row['description']."
Price: ". $row['price'];
}
}
thanks alot. I pray issues like this won't make me loose interest.
Re: PHP Masters Pls Help A PHP Learner Please. by Nobody: 12:57pm On Jul 27, 2013
adeaugustus:
thanks alot. I pray issues like this won't make me loose interest.

This shuld make you more determined! wink

1 Like

(1) (Reply)

Compiling From Source Codes / Check Out Grand Theft Auto Naija 3 For Android / Nigerian Developers & The Code Ninja Syndrome

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 23
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.