Jump to content

Recommended Posts

I'm just trying to get to grips with PHP it looks very flexible but still on the basics!

What I'm trying to do is I have two columns on a csv file, the first represents a destination and the second the number of places booked.

Basically as someone sends a form with their booking details these two fields are dropped onto the csv file.

What I want to achieve is to interrogate the csv file for a particular destination - say Spain and get a total number of places booked - ideally subtracting that figure from the total number of places available.

 

The end result would be an availability button on the associated "destination" page showing available places to that destination

 

I could probably do this using MySQL but as there are only ever around 8 destinations available at any time I thought that a CSV file was simplest.

I have managed to get the CSV into an alphabetical table format but I'm stuck now as to what to do next

Can someone point me in the right direction - do I have to use an array?

 

 

Many thanks

 

Learning all the time!

 

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/126403-solved-filter-and-formula-in-php/
Share on other sites

<?php
$destination = 'Spain';
$availablePlaces = 10;
$placesBooked = 0;

$entries = file('file.csv');

foreach ($entries as $entry) {
    $info = explode(',', $entry);
    
    if ($info[0] == $destination) {
        $placesBooked += $info[1];
    }
}

if ($placesBooked > $availablePlaces) {
    echo 'Somebody made rather a mess of things...';
}
else if ($placesBooked < $availablePlaces) {
    echo 'There are ' . $availablePlaces - $placesBooked . ' places left for ' . $destination;
}
else {
    echo 'Sorry, no places left for ' . $destination;
}
?>

 

Something like this?

It makes it look so easy!!! Thanks - I have input it but seem to be getting a slight error as even though I have spaces available I'm getting the message 0 places available in spain

Should I tabulate  my csv file first, then add this code, looking at it logically I presume the code works without tabulating first - in which case there must be something slightly wrong in my csv file, which when I view it reads as follows

 

"bognor","1"

"spain","2"

"spain","2"

"spain ","4"

so I should have got a reading of 2 places available - but thanks so far as I think it's much closer than I had got after 3 days of puzzling!!

Try:

<?php
$destination = 'Spain';
$availablePlaces = 10;
$placesBooked = 0;

$handle = fopen("test.txt", "r");
while (($info = fgetcsv($handle, 1000, ",")) !== FALSE) {
if (trim(strtolower($info[0])) == strtolower($destination)) {
	$placesBooked += $info[1];
}
}
fclose($handle);

if ($placesBooked > $availablePlaces) {
echo 'Somebody made rather a mess of things...';
}
else if ($placesBooked < $availablePlaces) {
echo 'There are ' . ($availablePlaces - $placesBooked) . ' places left for ' . $destination;
}
else {
echo 'Sorry, no places left for ' . $destination;
}
?>

 

That should give you "There are 2 places left for Spain" using the CSV sample you posted above.

Fantastic!!!!!

It works a treat!

Thanks so much - I think this convinced me that I must get down to seriously learning PHP as you certainly made that look easy!

The long dark nights of winter are going to be used this year!

 

Thanks again

 

Regards

 

While knowing some of the library functions and the syntax of the language may be important to some extent, it's much more important that you learn how to think like a programmer. Programming is about problem solving, so if you can do that, then you can program. Once you've grasped the language's syntax I'd recommend that you read books about programming theory instead of the "how to build a blog" kind of books/tutorials. There are various topics about books on this site. Just an advice :)

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.