Jump to content

NEED HELP WITH I/O FILE


rotten69

Recommended Posts

I'm using a simple shopping cart on my site and I want to implement functionality on the products page and details page (that contains details on the products basically) that reads the item information from an external file  e.g. a txt file and I don't want to use any Database systems.

 

So, for each item, I need to store:

1- the name

2- the image

3-the description

4-the price

 

and I want the products page to generate a new row in the table for each item stored in the file. And, the details file should use the file to retrieve the info that should display.

 

I think the best way of doing this is that implementing a function that reads the file into an array which can used by both the products page and details page.

 

I'm just new to PHP and want some ideas on where to start really ...

 

Cheers,

Link to comment
Share on other sites

read this http://www.w3schools.com/php/php_ref_filesystem.asp

 

Have a comma seperated file. Use fopen() - fgets() for single line input - explode to make array of single item - write HTML table code. Repeat till EOF. then fclose().

 

You should be able to write to a file, name the file copy delete and all kinds of good things to it.

 

w3schools good to use.

Link to comment
Share on other sites

Thanks for passing, buddy. I need an explanation on that if you can give one please.

 

And, also  what's a good idea to check if you have multiple usernames/ passwords in your log-in system?

 

 
session_start();
$users = array("user1" =>"infs", "user2" =>"csse", "user3" =>"maths", "user4"=>"mmds", "user5"=>"csse");
$passwords = array("password1"=>"3202" "password2"=>"2002" "password3"=>"1061" "password4"=>"1400" "password5"=>"1001");       <-- my last idea and I don't know how I'm gonna use arrays .. If I was to use these arrays then How Would I make a username from $users correspond to a matching password in $passwords. 

        
if($_REQUEST['username'] == "guru" && $_REQUEST['password'] == "3202") ||
	($_REQUEST['username'] == "mad" && $_REQUEST['password'] == "2002") ||
	($_REQUEST['username'] == "lolo" && $_REQUEST['password'] == "1001") ||
	($_REQUEST['username'] == "chap" && $_REQUEST['password'] == "1061") ||
	($_REQUEST['username'] == "pal" && $_REQUEST['password'] == "1400"){                                  <<-- this is my first idea and for

a username and password works, but for 5 sets of credentials doesn't work!

	$_SESSION['username'] = "guru" ;
	$_SESSION['password'] = "3202" ;

	$_SESSION['username'] = "mad" ;
	$_SESSION['password'] = "2002" ;

	$_SESSION['username'] = "lolo" ;
	$_SESSION['password'] = "1001" ;

	$_SESSION['username'] = "chap" ;
	$_SESSION['password'] = "1061" ;

	$_SESSION['username'] = "pal" ;
	$_SESSION['password'] = "1400" ;
	header("Location: home.php ");

 

 

 

Your help is appreciated.

Link to comment
Share on other sites

From above code it looks like your far more than a beginer. This may be too simple for you but here is an example of reading and displaying a cvs file with php:

<?php
$hd = fopen('personal.txt', 'r');
echo "<table border=\"1\" width=\"400px\"";
for($i = 0; $i < 5; $i++)
{
$line = fgets($hd);
$word = explode(',', $line);
echo '<tr>';
echo '<td>'.$word[0].'</td>';
echo '<td>'.$word[1].'</td>';
echo '<td>'.$word[2].'</td>';
echo '<td>'.$word[3].'</td>';
echo '<br />';
echo '</tr>';
}
fclose($hd);
?>

Link to comment
Share on other sites

hahha.. Thanks mate. Well, Was that a complement? lol

 

I modified the code you posted before and Here it is ..

 


<?php
$hd = fopen('content.txt', 'r');
$line = fgets($hd);
$content = array();

while($line)
{
	$word = explode(',', $line);
	$content [$word[0]] => array('img_path' => $word[1], 'description' => $word[2], 'price' => $word[3]);      // the error message is indicating this line is broken!!  Is it because of an equal sign missing? I tried putting an equal sign, but still it doesn't run.

	$line = fgets($hd);
}
fclose($hd);

?>

 

 

 

 

This is a log-in system I'm trying to implement 

 

session_start();
$users = array("user1" =>"3202", "user2" =>"2002", "user3" =>"1061", "user4"=>"1400", "user5"=>"1001");

//So, inside the if statement .... Would that be $_REQUEST or GET or even isSet?

if($_REQUEST['username'] == "user" && $_REQUEST['password'] == "3202"){  <-- This condition is NOT right.. and I don't really know how to re-set it up..   like how to check if the username is matching with its corresponding password


	$_SESSION['username'] = "user1" ;
	$_SESSION['password'] = "3202" ;

	$_SESSION['username'] = "user2" ;
	$_SESSION['password'] = "2002" ;

	$_SESSION['username'] = "user" ;
	$_SESSION['password'] = "1001" ;

	$_SESSION['username'] = "user3" ;
	$_SESSION['password'] = "1061" ;

	$_SESSION['username'] = "user4" ;
	$_SESSION['password'] = "1400" ;
	header("Location: home.php ");

 

Cheers,

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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