JADASDesigner Posted October 29, 2008 Share Posted October 29, 2008 <?php $row = 1; $handle = fopen("Book1.csv", "r"); while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $num = count($data); echo "<p> $num fields in line $row: <br /></p>\n"; $row++; for ($c=0; $c < $num; $c++) { echo $data[$c] . "<br />\n"; } } fclose($handle); ?> So I am trying to set up something that will call up the product id, product title, and price of a product. I have NO idea what I am doing, and this thing is spitting out all of the rows. I plan on doing something like this: <h1> <?php echo $producttitle ?> </h1> <h3> <?php echo $productid."<br> $".$productprice ?> </h3> and I need it to get the values for those variables from the CSV. I know I probably screwed that up, but I have been up for 36 hours, and I am out of red bull. My brain has stopped functioning. HELP!!! Quote Link to comment https://forums.phpfreaks.com/topic/130608-solved-clueless-on-csv/ Share on other sites More sharing options...
AndyB Posted October 29, 2008 Share Posted October 29, 2008 We/you need to know how each line of your data separates those variables. Then we can help you with split() or explode() - both in the manual. Give us a sample line. Quote Link to comment https://forums.phpfreaks.com/topic/130608-solved-clueless-on-csv/#findComment-677602 Share on other sites More sharing options...
JADASDesigner Posted October 29, 2008 Author Share Posted October 29, 2008 This is straight from the CSV: productid,producttitle,productprice SW45,3/4 oz. 17% Streetwise Pepper Spray with Clip and Key Ring ,11.99 SW3B,1/2oz 17% Streetwise Pepper Spray w/ Black Case,9.95 SW3A,1/2oz 17% Streetwise Pepper Spray w/ Assorted Color Case,9.95 SW3PP,1/2oz 17% Streetwise Pepper Spray W/ Pink Holster,9.95 SW3PC,1/2oz 17% Streetwise Pepper Spray W/ Pink Camouflage Holster ,9.95 Quote Link to comment https://forums.phpfreaks.com/topic/130608-solved-clueless-on-csv/#findComment-677625 Share on other sites More sharing options...
Maq Posted October 29, 2008 Share Posted October 29, 2008 $handle = @fopen("Book1.csv", "r"); if ($handle) { while (!feof($handle)) { $buffer = fgets($handle); $pieces = explode(",", $buffer); $productid = $pieces[0]; $producttitle = $pieces[1]; $productprice = $pieces[2]; ?> $".$productprice ?> } Quote Link to comment https://forums.phpfreaks.com/topic/130608-solved-clueless-on-csv/#findComment-677665 Share on other sites More sharing options...
JADASDesigner Posted October 29, 2008 Author Share Posted October 29, 2008 I do believe we are on the right track, but how do I get it to pull the individual records, so that <h1><?php echo $producttitle ?></h1> <h3><?php echo $productid."<br> $".$productprice ?></h3> turns into 3/4 oz. 17% Streetwise Pepper Spray with Clip and Key Ring SW45 $11.99 Quote Link to comment https://forums.phpfreaks.com/topic/130608-solved-clueless-on-csv/#findComment-677696 Share on other sites More sharing options...
Maq Posted October 29, 2008 Share Posted October 29, 2008 For individual records I would recommend putting these into a database and calling it from there with a query. That way you would have a unique identifier for each product and would have the ability to pull individual or groups of products out. Maybe someone else has a better idea. Quote Link to comment https://forums.phpfreaks.com/topic/130608-solved-clueless-on-csv/#findComment-677703 Share on other sites More sharing options...
JADASDesigner Posted October 29, 2008 Author Share Posted October 29, 2008 TBH, I know absolutly nothing about how to use or manage a database. Ideally, I would like the user to beable to click on http://www.example.com/pepper_spray/index.php?productid=sw45, and it kick them into a page that has the info as listed previously. If there is an easy way to add a $productdescription, that would be a life saver. I am trying to get everything I need done by the first, and this is the only thing in my way. Quote Link to comment https://forums.phpfreaks.com/topic/130608-solved-clueless-on-csv/#findComment-677720 Share on other sites More sharing options...
Maq Posted October 29, 2008 Share Posted October 29, 2008 I guess you don't need a database but this method is going to be slow with a lot of products. $get_id = $_GET['product_id']; $handle = @fopen("Book1.csv", "r"); if ($handle) { while (!feof($handle)) { $buffer = fgets($handle); $pieces = explode(",", $buffer); $productid = $pieces[0]; $producttitle = $pieces[1]; $productprice = $pieces[2]; if($productid == $get_id) { ?> $".$productprice ?> } } Quote Link to comment https://forums.phpfreaks.com/topic/130608-solved-clueless-on-csv/#findComment-677746 Share on other sites More sharing options...
JADASDesigner Posted October 29, 2008 Author Share Posted October 29, 2008 It looks like the code got jumbled somewhere. could you repaste that? Quote Link to comment https://forums.phpfreaks.com/topic/130608-solved-clueless-on-csv/#findComment-677753 Share on other sites More sharing options...
Maq Posted October 29, 2008 Share Posted October 29, 2008 It looks like the code got jumbled somewhere. could you repaste that? Huh? Ok. I guess you don't need a database but this method is going to be slow with a lot of products. $get_id = $_GET['product_id']; $handle = @fopen("Book1.csv", "r"); if ($handle) { while (!feof($handle)) { $buffer = fgets($handle); $pieces = explode(",", $buffer); $productid = $pieces[0]; $producttitle = $pieces[1]; $productprice = $pieces[2]; if($productid == $get_id) { ?> $".$productprice ?> } } Quote Link to comment https://forums.phpfreaks.com/topic/130608-solved-clueless-on-csv/#findComment-677787 Share on other sites More sharing options...
JADASDesigner Posted October 29, 2008 Author Share Posted October 29, 2008 When I paste $get_id = $_GET['product_id']; $handle = @fopen("Book1.csv", "r"); if ($handle) { while (!feof($handle)) { $buffer = fgets($handle); $pieces = explode(",", $buffer); $productid = $pieces[0]; $producttitle = $pieces[1]; $productprice = $pieces[2]; if($productid == $get_id) { ?> <h1><?php echo $producttitle ?></h1> <h3><?php echo $productid."<br> $".$productprice ?></h3> <?php } } } I get: Parse error: syntax error, unexpected '}' in /home8/jadasdes/public_html/pp/SW45mini.php on line 16 Quote Link to comment https://forums.phpfreaks.com/topic/130608-solved-clueless-on-csv/#findComment-677793 Share on other sites More sharing options...
Maq Posted October 29, 2008 Share Posted October 29, 2008 Can you post the entire code? Quote Link to comment https://forums.phpfreaks.com/topic/130608-solved-clueless-on-csv/#findComment-677798 Share on other sites More sharing options...
JADASDesigner Posted October 29, 2008 Author Share Posted October 29, 2008 That is the entire code. Quote Link to comment https://forums.phpfreaks.com/topic/130608-solved-clueless-on-csv/#findComment-677802 Share on other sites More sharing options...
Maq Posted October 29, 2008 Share Posted October 29, 2008 Try putting <?php at the top before you use php code... Quote Link to comment https://forums.phpfreaks.com/topic/130608-solved-clueless-on-csv/#findComment-677807 Share on other sites More sharing options...
JADASDesigner Posted October 29, 2008 Author Share Posted October 29, 2008 I did (forgot to paste that high). Do you think there may be somethign wrong on the server end? I need to get some rest. I have been looking at this screen for WAY too long. Knowing my luck, whenever I happen to wake back up, the solution will be right in front of me... Quote Link to comment https://forums.phpfreaks.com/topic/130608-solved-clueless-on-csv/#findComment-677813 Share on other sites More sharing options...
Maq Posted October 29, 2008 Share Posted October 29, 2008 I have not tested this, but it looks like it should work, try it without the HTML crap maybe I was forgetting to open/close something. $get_id = $_GET['product_id']; $handle = @fopen("Book1.csv", "r"); if ($handle) { while (!feof($handle)) { $buffer = fgets($handle); $pieces = explode(",", $buffer); $productid = $pieces[0]; $producttitle = $pieces[1]; $productprice = $pieces[2]; if($productid == $get_id) { echo $producttitle; echo $productid; echo $productprice; } } } Quote Link to comment https://forums.phpfreaks.com/topic/130608-solved-clueless-on-csv/#findComment-677836 Share on other sites More sharing options...
JADASDesigner Posted October 29, 2008 Author Share Posted October 29, 2008 Now I am getting a blank page with no errors. Quote Link to comment https://forums.phpfreaks.com/topic/130608-solved-clueless-on-csv/#findComment-677851 Share on other sites More sharing options...
Maq Posted October 30, 2008 Share Posted October 30, 2008 Yes I know because you have to pass $_GET['product_id'] through the URL. You said when a user clicks on: "http://www.example.com/pepper_spray/index.php?productid=sw45" they get the results for SW45. So when you pass variables with the GET method they tag on to the end of the URL and you retrieve them with $_GET[]. Try this: //$get_id = $_GET['productid']; $get_id = "SW45"; $handle = @fopen("Book1.csv", "r"); if ($handle) { while (!feof($handle)) { $buffer = fgets($handle); $pieces = explode(",", $buffer); $productid = $pieces[0]; $producttitle = $pieces[1]; $productprice = $pieces[2]; if($productid == $get_id) { echo $producttitle; echo $productid; echo $productprice; } } } Quote Link to comment https://forums.phpfreaks.com/topic/130608-solved-clueless-on-csv/#findComment-678485 Share on other sites More sharing options...
JADASDesigner Posted October 30, 2008 Author Share Posted October 30, 2008 TYTY. No how do I mark this thing solved... Quote Link to comment https://forums.phpfreaks.com/topic/130608-solved-clueless-on-csv/#findComment-678553 Share on other sites More sharing options...
Maq Posted October 30, 2008 Share Posted October 30, 2008 TYTY. No how do I mark this thing solved... LOL, bottom left. Quote Link to comment https://forums.phpfreaks.com/topic/130608-solved-clueless-on-csv/#findComment-678559 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.