Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Page 1 of 4 1 2 3 ... LastLast
Results 1 to 10 of 40
  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    27
    Member #
    31494
    Hi

    I am designing a property website and have put some properties in a excel spreadhseet and saved it as a csv file and have used php coding to retrieve the data and display it on my webpage but how do I format the page so it is displayed in a nice table with borders

    The coding is below

    PHP Code:
    <?php
    echo "<html><body><table>\n\n";
    $f fopen("properties.csv""r");
    while ((
    $line fgetcsv($f)) !== false) {
            echo 
    "<tr>";
            foreach (
    $line as $cell) {
                    echo 
    "<td>" htmlspecialchars($cell) . "</td>";
            }
            echo 
    "<tr>\n";
    }
    fclose($f);
    echo 
    "\n</table></body></html>";

  2.  

  3. #2
    Senior Member Ganners's Avatar
    Join Date
    Feb 2011
    Location
    United Kingdom
    Posts
    415
    Member #
    27007
    Liked
    92 times
    Try something like this (style copied from http://tympanus.net/codrops/2010/05/...les-with-css3/)

    I reorganised it a bit, when working with HTML I like to use nested php inside the HTML where possible - turning it into as much of a 'template' as possible. I don't know if you have included titles in your properties.csv, if you have then this will provide some cool styling!

    PHP Code:
    <?php

    /**
    * Reads in a CSV and outputs it to a table
    */

    //Opens the CSV
    $f fopen("properties.csv""r");

    $csv_contents = array();

    //Produces headers if true
    $header_exists true;

    //Produce array from contents
    while (($line fgetcsv($f)) !== false) {
        
    $csv_contents[] = $line;
    }

    //If the header exists then we split it up
    if($header_exists) {
        
    $header $csv_contents[0];
        
    array_shift($csv_contents);
    }

    //Close file
    fclose($f);

    ?>
    <!doctype html>
    <html>
        <head>
            <title>CSV to Table</title>
            <style type="text/css">
                #csv thead th{
                    padding:15px;
                    color:#fff;
                    text-shadow:1px 1px 1px #568F23;
                    border:1px solid #93CE37;
                    border-bottom:3px solid #9ED929;
                    background-color:#9DD929;
                    background:-webkit-gradient(
                        linear,
                        left bottom,
                        left top,
                        color-stop(0.02, rgb(123,192,67)),
                        color-stop(0.51, rgb(139,198,66)),
                        color-stop(0.87, rgb(158,217,41))
                        );
                    background: -moz-linear-gradient(
                        center bottom,
                        rgb(123,192,67) 2%,
                        rgb(139,198,66) 51%,
                        rgb(158,217,41) 87%
                        );
                    -webkit-border-top-left-radius:5px;
                    -webkit-border-top-right-radius:5px;
                    -moz-border-radius:5px 5px 0px 0px;
                    border-top-left-radius:5px;
                    border-top-right-radius:5px;
                }

                #csv thead th:empty{
                    background:transparent;
                    border:none;
                }
                #csv tfoot td{
                    color: #9CD009;
                    font-size:32px;
                    text-align:center;
                    padding:10px 0px;
                    text-shadow:1px 1px 1px #444;
                }
                #csv tfoot th{
                    color:#666;
                }
                #csv tbody td{
                    padding:10px;
                    text-align:center;
                    background-color:#DEF3CA;
                    border: 2px solid #E7EFE0;
                    -moz-border-radius:2px;
                    -webkit-border-radius:2px;
                    border-radius:2px;
                    color:#666;
                    text-shadow:1px 1px 1px #fff;
                }
            </style>
        </head>
        <body>
            <table id="csv">
                <?php if(isset($header)) { ?>
                <thead>
                    <tr>
                        <?php foreach($header as $key => $value) { ?>
                        <th><?php echo htmlspecialchars($value); ?></th>
                        <?php // end foreach ?>
                    </tr>
                </thead>
                <?php //end if ?>
                <tbody>
                    <?php foreach($csv_contents as $line) { ?>

                        <tr>
                        <?php foreach ($line as $key => $value) { ?>
                            <td><?php echo htmlspecialchars($value); ?></td>
                        <?php //end foreach ?>
                        </tr>
                    <?php // end foreach ?>
                </tbody>
                <?php if(isset($header)) { ?>
                <tfoot>
                    <tr>
                        <?php foreach($header as $key => $value) { ?>
                        <th><?php echo htmlspecialchars($value); ?></th>
                        <?php // end foreach ?>
                    </tr>
                </tfoot>
                <?php //end if ?>
            </table>
        </body>
    </html>
    Mark Gannaway Software Developer

    Recent Experiments
    - Backpropogation Neural Network language solving (http://ann.ganners.co.uk/)
    - Animated image to ASCII (http://google.ganners.co.uk/)
    - 3D Paper Characters (http://cybergame.ganners.co.uk/)
    - Anagram solving (http://roflol.co.uk/)

  4. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    27
    Member #
    31494
    Hi Ganners

    Thank you for the reply and the coding supplied, appreciate it so much

    I do have headings in the csv file, cant wait to test the coding, just gonna try it out now

    also when this part works and looks nice, do you know if its possible to display results from the csv file based on a user search as its a property website, I can provide the coding for the form if need be

    Thank you so much again

    Ian

  5. #4
    Junior Member
    Join Date
    Apr 2012
    Posts
    27
    Member #
    31494
    the coding worked lovely, looks so much better now

    Thank you so much

    do you know if its possible in regards to my question regarding the csv file displaying results based on the user search

    Kind regards

    Ian

  6. #5
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,717
    Member #
    5580
    Liked
    718 times
    Maybe you would rather use Google Docs Spreadsheet to list your properties (instead of Excel)?

    If you do that (and it's free with any Google Account), you can alter your spreadsheet online
    from any PC anywhere, and you can assign other people to alter data. Then, PHP can
    grab your spreadsheet data and display things on your website dynamically.


  7. #6
    Junior Member
    Join Date
    Apr 2012
    Posts
    27
    Member #
    31494
    Hi mlseim

    ok cool, I had trouble with PHP before though when I tried getting the PHP to connect to the MySQL database, it took me a while to get connected to the database and then it wouldnt display the results correctly and couldnt get it to work to display the results based on a seach made by the user

  8. #7
    Senior Member Ganners's Avatar
    Join Date
    Feb 2011
    Location
    United Kingdom
    Posts
    415
    Member #
    27007
    Liked
    92 times
    Google docs is a good option, you can of course perform searches of the read CSV file, that's not a problem and can be done with a little array magic. There are other options as well though! JavaScript is is a good one, there are some jQuery plugins for example which allow searching of a table. Check this out:

    http://lomalogue.com/jquery/quicksearch/

    If you'd like to use PHP then just say and I can provide you a code snippit for that.
    Mark Gannaway Software Developer

    Recent Experiments
    - Backpropogation Neural Network language solving (http://ann.ganners.co.uk/)
    - Animated image to ASCII (http://google.ganners.co.uk/)
    - 3D Paper Characters (http://cybergame.ganners.co.uk/)
    - Anagram solving (http://roflol.co.uk/)

  9. #8
    Junior Member
    Join Date
    Apr 2012
    Posts
    27
    Member #
    31494
    yeah thank you so much Ganners, I will check the link out

    I did upload my properties.csv file to google docs but that's as far as I got as kinda unsure on the how to connect to the spreadsheet and display the data on my webpage based on what a user searches for property wise

  10. #9
    Junior Member
    Join Date
    Apr 2012
    Posts
    27
    Member #
    31494
    Hi Ganners

    Just had a look at the link and like the top table and have got the following code

    Code:
    $('input#id_search').quicksearch('table#table_example tbody tr');
    Where abouts would I insert that in the coding you did bit earlier

  11. #10
    Senior Member Ganners's Avatar
    Join Date
    Feb 2011
    Location
    United Kingdom
    Posts
    415
    Member #
    27007
    Liked
    92 times
    If you're not looking for a learning curve then I wouldn't bother with google docs for now, but bare it in mind! There are clever API's for it's use.

    The above code is JavaScript and is to be used with the jQuery framework to allow client side searching. If you aren't familiar with jQuery or JavaScript then again I wouldn't bother with this for now. But do look into it as it is a really cool and quick solution.

    For now we can just use PHP and some array filtering to search each line for a given string. Try the following bit of code:

    PHP Code:
    <?php

    /**
    * Reads in a CSV and outputs it to a table
    */

    //Opens the CSV
    $f fopen("properties.csv""r");

    $csv_contents = array();

    //Produces headers if true
    $header_exists true;

    //Produce array from contents
    while (($line fgetcsv($f)) !== false) {
    $csv_contents[] = $line;
    }

    //If the header exists then we split it up
    if($header_exists) {
    $header $csv_contents[0];
    array_shift($csv_contents);
    }

    //Close file
    fclose($f);

    //Perform search if search query string is set
    if(isset($_GET['search']) && $_GET['search'] != NULL) {
    $csv_contents filterArray($csv_contents$_GET['search']);
    }

    ?>
    <!doctype html>
    <html>
    <head>
    <title>CSV to Table</title>
    <style type="text/css">
    #csv thead th{
    padding:15px;
    color:#fff;
    text-shadow:1px 1px 1px #568F23;
    border:1px solid #93CE37;
    border-bottom:3px solid #9ED929;
    background-color:#9DD929;
    background:-webkit-gradient(
    linear,
    left bottom,
    left top,
    color-stop(0.02, rgb(123,192,67)),
    color-stop(0.51, rgb(139,198,66)),
    color-stop(0.87, rgb(158,217,41))
    );
    background: -moz-linear-gradient(
    center bottom,
    rgb(123,192,67) 2%,
    rgb(139,198,66) 51%,
    rgb(158,217,41) 87%
    );
    -webkit-border-top-left-radius:5px;
    -webkit-border-top-right-radius:5px;
    -moz-border-radius:5px 5px 0px 0px;
    border-top-left-radius:5px;
    border-top-right-radius:5px;
    }

    #csv thead th:empty{
    background:transparent;
    border:none;
    }
    #csv tfoot td{
    color: #9CD009;
    font-size:32px;
    text-align:center;
    padding:10px 0px;
    text-shadow:1px 1px 1px #444;
    }
    #csv tfoot th{
    color:#666;
    }
    #csv tbody td{
    padding:10px;
    text-align:center;
    background-color:#DEF3CA;
    border: 2px solid #E7EFE0;
    -moz-border-radius:2px;
    -webkit-border-radius:2px;
    border-radius:2px;
    color:#666;
    text-shadow:1px 1px 1px #fff;
    }
    </style>
    </head>
    <body>
    <table id="csv">
    <?php if(isset($header)) { ?>
    <thead>
    <tr>
    <?php foreach($header as $key => $value) { ?>
    <th><?php echo htmlspecialchars($value); ?></th>
    <?php // end foreach ?>
    </tr>
    </thead>
    <?php //end if ?>
    <tbody>
    <?php if(!empty($csv_contents)) { ?>
    <?php 
    foreach($csv_contents as $line) { ?>
    <tr>
    <?php if(!empty($line)) { ?>
    <?php 
    foreach ($line as $key => $value) { ?>
    <td><?php echo htmlspecialchars($value); ?></td>
    <?php //end foreach ?>
    <?php 
    //end if ?>
    </tr>
    <?php // end foreach ?>
    <?php 
    //end if ?>
    </tbody>
    <?php if(isset($header)) { ?>
    <tfoot>
    <tr>
    <?php foreach($header as $key => $value) { ?>
    <th><?php echo htmlspecialchars($value); ?></th>
    <?php // end foreach ?>
    </tr>
    </tfoot>
    <?php //end if ?>
    </table>

    <form method="get" id="csv-filter">
    <input type="text" name="search" value="" />
    <input type="submit" value="Search" />
    </form>
    </body>
    </html>

    <?php

    /**
    * Filters a csv line by a single search string and returns any line that contains it. Case insensitive
    */

    function filterArray($haystack$searchterm) {

    $filteredArray = (array) NULL;

    foreach(
    $haystack as $key => $value) {
    foreach(
    $value as $akey => $avalue) {
    if(
    strstr(strtolower($avalue), strtolower($searchterm))) {
    $filteredArray[] = $haystack[$key];
    unset(
    $haystack[$key]);
    }
    }
    }

    return (array) 
    $filteredArray;

    }

    ?>
    It will check for a query string ($_GET) in the URL, and if it finds it, will use that as a search and return any line that contains it.
    Mark Gannaway Software Developer

    Recent Experiments
    - Backpropogation Neural Network language solving (http://ann.ganners.co.uk/)
    - Animated image to ASCII (http://google.ganners.co.uk/)
    - 3D Paper Characters (http://cybergame.ganners.co.uk/)
    - Anagram solving (http://roflol.co.uk/)


Page 1 of 4 1 2 3 ... LastLast

Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Search tags for this page

php filter row from csv

Click on a term to search for related topics.
All times are GMT -6. The time now is 12:34 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com