Jump to content

[SOLVED] Clueless on CSV


JADASDesigner

Recommended Posts

<?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!!!

Link to comment
https://forums.phpfreaks.com/topic/130608-solved-clueless-on-csv/
Share on other sites

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

 

$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 ?>
      
}

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

 

 

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.

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.

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 ?>
             }
}

 

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 ?>
             }
}

 

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

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...

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

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

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.