/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Function problem

Hi all

I’m building a boat brokerage website. Each boat has its information stored in a flat .txt file in the format ENGINE=foo, PRICE=bar, DESCRIPTION=lorem, etc. Each boat has its own text file. The main page page should count the number of text files (boats), and then enter a loop to draw up a table for each boat (text file) and populate it with the information from the file (engine, etc.)

So far, this works great on the ‘details’ page where it is only pulling info from one file and populating that page. The problem comes on the index page where I want it to pull some summary info from EVERY text file. I don’t know what’s going wrong but if someone could help me, I’d be eternally grateful!

I currently have just two text files there for testing purposes.

The page itself is at: [url]http://www.broadsboatsales.co.uk/beta/motorboats[/url]

And the code is:

[CODE] <?php

if(!function_exists(‘get_value_of’));
function get_value_of($name) {
// by this point, it’s forgotten the value for
// $filename and returns null so doesn’t display anything
$lines = file(‘products/’ . $boatfile);
foreach (array_values($lines) AS $line) {
list($key, $val) = explode(‘=’, trim($line) );
if (trim($key) == $name) {
return $val; } }
return false; }

$directory = “products/”;

//get all text files with a .txt extension.
$textfiles = glob(“” . $directory . “*.txt”);

//print each file name
foreach($textfiles as $textfile)

{

$boatfile = substr($textfile, 9);
// $boatfile = $textfile;

// read the correct text file for first boat

// draw each table and insert data from appropriate text file using boatfile as reference

// a line to test it’s getting the right file (which it is)
echo(“Backend filename: ” . $boatfile);

// draw the table and populate it with the info from the function above

print(‘<table width=”100&#37;” cellspacing=”0″ cellpadding=”3″ class=”products”><tr><td width=”85%” align=”left” bgcolor=”#FFFFCC” class=”producttitle”>&nbsp;’ . get_value_of(‘LONGTITLE’) . ‘</td><td width=”15%” align=”right” bgcolor=”#FFFFCC” class=”productprice”>&#163;’ . get_value_of(‘PRICE’) . ‘</td></tr><tr><td colspan=”2″><table width=”100%” border=”0″ cellspacing=”6″ cellpadding=”0″><tr><td width=”245″ align=”center” valign=”middle”><img src=”products/’ . get_value_of(‘FOLDER’) . ‘thumb.JPG” width=”245″ height=”163″ /></td><td align=”left” valign=”top”><table width=”100%” border=”0″ cellspacing=”6″ cellpadding=”0″><tr><td colspan=”2″ class=”productdescription”>’ . get_value_of(‘EXTRACT’) . ‘</td></tr><tr><td height=”1″ colspan=”2″ bgcolor=”#999999″></td></tr><tr><td width=”27%” valign=”middle” class=”productspecshdr”>&nbsp;&nbsp;&nbsp;<a rel=”tooltip” title=”Outboards are more easily accessible and serviceable. Inboards offer better refinement and potentially more power. Generally, smaller engines are petrol, with larger being diesel.”><img src=”img/info_icon.png” alt=”” width=”12″ height=”12″ border=”0″ align=”absmiddle” /></a>&nbsp; Engine </td><td width=”73%” class=”productspecsdetails”>’ . get_value_of(‘ENGINE’) . ‘</td></tr><tr><td valign=”middle” class=”productspecshdr”>&nbsp;&nbsp;&nbsp;<a rel=”tooltip” title=”Beds, in layman’s terms.”><img src=”img/info_icon.png” alt=”” width=”12″ height=”12″ align=”absmiddle” /></a> &nbsp;Berth</td><td class=”productspecsdetails”>’ . get_value_of(‘BERTH’) . ‘</td></tr><tr><td valign=”middle” class=”productspecshdr”>&nbsp;&nbsp;&nbsp;<a rel=”tooltip” title=”Essentially a bedroom – individual sleeping areas.”><img src=”img/info_icon.png” alt=”” width=”12″ height=”12″ align=”absmiddle” /></a> &nbsp;Cabins </td><td class=”productspecsdetails”>’ . get_value_of(‘CABINS’) . ‘</td></tr><tr><td valign=”middle” class=”productspecshdr”>&nbsp;&nbsp;&nbsp;<a rel=”tooltip” title=”Measured bow to stern (front to back).”><img src=”img/info_icon.png” alt=”” width=”12″ height=”12″ align=”absmiddle” /></a>&nbsp;&nbsp;Length </td><td class=”productspecsdetails”>’ . get_value_of(‘LENGTH’) . ‘</td></tr><tr><td valign=”middle” class=”productspecshdr”>&nbsp;&nbsp;&nbsp;<a rel=”tooltip” title=”The width from port to starboard (left to right).”><img src=”img/info_icon.png” alt=”” width=”12″ height=”12″ align=”absmiddle” /></a> &nbsp;Beam </td><td class=”productspecsdetails”>’ . get_value_of(‘BEAM’) . ‘</td></tr><tr><td colspan=”2″ align=”right” class=”productspecshdr”><a href=”../contact?ref=’ . get_value_of(‘TITLE’) . ‘”>Enquire</a> &nbsp;| &nbsp;<a href=”details.php?ref=’ . $boatfile . ‘”>View Details &gt;</a></td></tr></table></td></tr></table></td></tr></table><br />’);

}
?>[/CODE]

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@benwestonauthorJun 23.2011 — Anyone?! It's one of those things I've been looking at for about 8 hours and cannot find a solution to!!
Copy linkTweet thisAlerts:
@NvenomJun 23.2011 — the best i can advise is switch to a MySql DB easier to add information and in this case grab it (atleast for me) as I can not find the issue in your code, although I am sure someone more experienced can.

If you need any help doing it just ask.
Copy linkTweet thisAlerts:
@criterion9Jun 23.2011 — the best i can advise is switch to a mysql DB easier to add information and in this case grab it (atleast for me) as i can not find the issue in your code, although im sure somone more experienced can[/QUOTE]

+1. This type of arrangement just screams "put me in a database".
Copy linkTweet thisAlerts:
@benwestonauthorJun 23.2011 — I agree. Trouble is, the host doesn't provide MySQL (!!) and the client doesn't want to switch hosts! This pretty much forces me to flat files! I'm tearing my hair out trying to find the issue - it's one of those things I've been over so many times and fail to see the problem! It's not even as if PHP is throwing any errors - it just doesn't seem to be passing the $boatfile string back to the get_value_of function!
Copy linkTweet thisAlerts:
@NvenomJun 23.2011 — Okay, hmmm tuff customer i supose lol.

A few things I would try,

  • 1. See if you can print just one perfectly, im sure you have already done that. ?


  • 2. See if the error is not in that script but the way it reads the text files.


  • A question, are you displaying all this info in multiple text files or just one .txt?

    if its just one could you not just open it up and format it so its possible to loop it?

    Maybe you are looping if so i just dont see it or its really over my head lol XD
    Copy linkTweet thisAlerts:
    @benwestonauthorJun 23.2011 — Thanks for your reply :-) Yes, if I alter the $boatfile string in the get_value_of function:

    $lines = file('products/' . $boatfile);

    to a direct text file (i.e. boat1.txt), it will draw all the boxes as it should and then populate them all with the data from boat1.txt as you would expect. The problem seems to lay in updating the function with the next file's name (i.e. boat2.txt, boat3.txt) each time the loop is run. $boatfile *should* contain the appropriate filename each time it's run but whenever the get_value_of function calls it, it's null. If I echo the $boatfile string directly before, or after, the get_value_of function, it displays the correct filename. The problem seems to be that it is always null when called by the function itself.

    I hope that makes some sense!!
    Copy linkTweet thisAlerts:
    @criterion9Jun 23.2011 — I don't know if it is too late in the process...but... using XML might prove easier to manage especially if the stored data might change. Plus you can use simpleXML and not have to mess with i/o at all...
    Copy linkTweet thisAlerts:
    @NvenomJun 24.2011 — -Reposting it so its more readable, at least to me-
    [code=php]
    <?php

    if(!function_exists('get_value_of'));
    function get_value_of($name)
    {
    // by this point, it's forgotten the value for
    // $filename and returns null so doesn't display anything
    $lines = file('products/' . $boatfile);
    foreach (array_values($lines) AS $line)
    {
    list($key, $val) = explode('=', trim($line) );

    if (trim($key) == $name)
    {
    return $val;
    }
    }
    return false;
    }

    $directory = "products/";

    //get all text files with a .txt extension.
    $textfiles = glob("" . $directory . "*.txt");


    //print each file name
    foreach($textfiles as $textfile)

    {


    $boatfile = substr($textfile, 9);
    // $boatfile = $textfile;

    // read the correct text file for first boat

    // draw each table and insert data from appropriate text file using boatfile as reference


    // a line to test it's getting the right file (which it is)
    echo("Backend filename: " . $boatfile);

    // draw the table and populate it with the info from the function above
    print('<table width="100%" cellspacing="0" cellpadding="3" class="products"><tr><td width="85%" align="left" bgcolor="#FFFFCC" class="producttitle">&nbsp;' . get_value_of('LONGTITLE') . '</td><td width="15%" align="right" bgcolor="#FFFFCC" class="productprice">£' . get_value_of('PRICE') . '</td></tr><tr><td colspan="2"><table width="100%" border="0" cellspacing="6" cellpadding="0"><tr><td width="245" align="center" valign="middle"><img src="products/' . get_value_of('FOLDER') . 'thumb.JPG" width="245" height="163" /></td><td align="left" valign="top"><table width="100%" border="0" cellspacing="6" cellpadding="0"><tr><td colspan="2" class="productdescription">' . get_value_of('EXTRACT') . '</td></tr><tr><td height="1" colspan="2" bgcolor="#999999"></td></tr><tr><td width="27%" valign="middle" class="productspecshdr">&nbsp;&nbsp;&nbsp;<a rel="tooltip" title="Outboards are more easily accessible and serviceable. Inboards offer better refinement and potentially more power. Generally, smaller engines are petrol, with larger being diesel."><img src="img/info_icon.png" alt="" width="12" height="12" border="0" align="absmiddle" /></a>&nbsp; Engine </td><td width="73%" class="productspecsdetails">' . get_value_of('ENGINE') . '</td></tr><tr><td valign="middle" class="productspecshdr">&nbsp;&nbsp;&nbsp;<a rel="tooltip" title="Beds, in layman's terms."><img src="img/info_icon.png" alt="" width="12" height="12" align="absmiddle" /></a> &nbsp;Berth</td><td class="productspecsdetails">' . get_value_of('BERTH') . '</td></tr><tr><td valign="middle" class="productspecshdr">&nbsp;&nbsp;&nbsp;<a rel="tooltip" title="Essentially a bedroom - individual sleeping areas."><img src="img/info_icon.png" alt="" width="12" height="12" align="absmiddle" /></a> &nbsp;Cabins </td><td class="productspecsdetails">' . get_value_of('CABINS') . '</td></tr><tr><td valign="middle" class="productspecshdr">&nbsp;&nbsp;&nbsp;<a rel="tooltip" title="Measured bow to stern (front to back)."><img src="img/info_icon.png" alt="" width="12" height="12" align="absmiddle" /></a>&nbsp;&nbsp;Length </td><td class="productspecsdetails">' . get_value_of('LENGTH') . '</td></tr><tr><td valign="middle" class="productspecshdr">&nbsp;&nbsp;&nbsp;<a rel="tooltip" title="The width from port to starboard (left to right)."><img src="img/info_icon.png" alt="" width="12" height="12" align="absmiddle" /></a> &nbsp;Beam </td><td class="productspecsdetails">' . get_value_of('BEAM') . '</td></tr><tr><td colspan="2" align="right" class="productspecshdr"><a href="../contact?ref=' . get_value_of('TITLE') . '">Enquire</a> &nbsp;| &nbsp;<a href="details.php?ref=' . $boatfile . '">View Details &gt;</a></td></tr></table></td></tr></table></td></tr></table><br />');

    }
    ?>
    [/code]


    Yeah your function dose not make much sense to me, seems as though its missing some things (like an else) but i am not 100% sure. so i tried reformatting, doubt it will make a difference but you never know ?

    [code=php]
    <?php
    if(!function_exists('get_value_of')){
    function get_value_of($name)
    {
    $lines = file('products/' . $boatfile);
    foreach (array_values($lines) AS $line)
    {
    list($key, $val) = explode('=', trim($line) );

    if (trim($key) == $name)
    {
    return $val;
    }
    }
    }
    }
    else
    {
    return false;
    echo "Failed to Grab Value";
    }
    ?>
    [/code]


    that is the only thing i can seem to see so if none of that fixes it im sorry to say there is nothing i can do, problem seems to far out of my reach
    Copy linkTweet thisAlerts:
    @benwestonauthorJun 24.2011 — Thanks for that - got me headed in the right direction! The problem WAS the function itself so I ended up approaching it another way. Here's the code for anyone who might Google this thread:

    [code=php]<?php

    //get all text files with a .txt extension.
    $textfiles = glob("products/" . "*.txt");


    //print each file name
    foreach($textfiles as $textfile)

    {
    $boatfile = substr($textfile, 9);

    $file_handle = fopen("products/" . $boatfile, "rb");

    while (!feof($file_handle) ) {

    $line_of_text = fgets($file_handle);
    $parts = explode('=', $line_of_text);

    // assign variables to strings

    $varname = $parts[0];
    $varcontent = $parts[1];
    $$varname = $varcontent;

    }

    fclose($file_handle);

    // draw the table and populate it with the info from the function above

    print('<table width="100&#37;" cellspacing="0" cellpadding="3" class="products"><tr><td width="85%" align="left" bgcolor="#FFFFCC" class="producttitle">&nbsp;' . $LONGTITLE . '</td><td width="15%" align="right" bgcolor="#FFFFCC" class="productprice">&#163;' . $PRICE . '</td></tr><tr><td colspan="2"><table width="100%" border="0" cellspacing="6" cellpadding="0"><tr><td width="245" align="center" valign="middle"><img src="products/' . $FOLDER . 'thumb.jpg" width="245" height="163" /></td><td align="left" valign="top"><table width="100%" border="0" cellspacing="6" cellpadding="0"><tr><td colspan="2" class="productdescription">' . $EXTRACT . '</td></tr><tr><td height="1" colspan="2" bgcolor="#999999"></td></tr><tr><td width="27%" valign="middle" class="productspecshdr">&nbsp;&nbsp;&nbsp;<a rel="tooltip" title="Outboards are more easily accessible and serviceable. Inboards offer better refinement and potentially more power. Generally, smaller engines are petrol, with larger being diesel."><img src="img/info_icon.png" alt="" width="12" height="12" border="0" align="absmiddle" /></a>&nbsp; Engine </td><td width="73%" class="productspecsdetails">' . $ENGINE . '</td></tr><tr><td valign="middle" class="productspecshdr">&nbsp;&nbsp;&nbsp;<a rel="tooltip" title="Beds, in layman's terms."><img src="img/info_icon.png" alt="" width="12" height="12" align="absmiddle" /></a> &nbsp;Berth</td><td class="productspecsdetails">' . $BERTH . '</td></tr><tr><td valign="middle" class="productspecshdr">&nbsp;&nbsp;&nbsp;<a rel="tooltip" title="Essentially a bedroom - individual sleeping areas."><img src="img/info_icon.png" alt="" width="12" height="12" align="absmiddle" /></a> &nbsp;Cabins </td><td class="productspecsdetails">' . $CABINS . '</td></tr><tr><td valign="middle" class="productspecshdr">&nbsp;&nbsp;&nbsp;<a rel="tooltip" title="Measured bow to stern (front to back)."><img src="img/info_icon.png" alt="" width="12" height="12" align="absmiddle" /></a>&nbsp;&nbsp;Length </td><td class="productspecsdetails">' . $LENGTH . '</td></tr><tr><td valign="middle" class="productspecshdr">&nbsp;&nbsp;&nbsp;<a rel="tooltip" title="The width from port to starboard (left to right)."><img src="img/info_icon.png" alt="" width="12" height="12" align="absmiddle" /></a> &nbsp;Beam</td><td class="productspecsdetails">' . $BEAM . '</td></tr><tr><td colspan="2" align="right" class="productspecshdr"><a href="../contact?ref=' . $TITLE . '">Enquire</a> &nbsp;| &nbsp;<a href="details.php?ref=' . $boatfile . '">View Details &gt;</a></td></tr></table></td></tr></table></td></tr></table><br />');





    }
    ?> [/code]
    Copy linkTweet thisAlerts:
    @NvenomJun 24.2011 — well glad i helped out in some way ? dont forget to make your thread [Resloved] using the thread tools at the top
    ×

    Success!

    Help @benweston spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.17,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @nearjob,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,
    )...