rusbb Posted June 7, 2007 Share Posted June 7, 2007 Hi all, I'm need a rather simple script which will do the following: Open a text file which will look something like this: Link 1 http://www.blahblah.com/1651561 Link 2 http://www.etc.com/34324 Link 3 http://www.sdfkjsdf.com/12343423 basically its a "Link n" text followed by a URL text in the format of "http://www.page.com/random numbers" What I would like the php script to do is to read "Link n" text and output it with the a http link to the URL which follows it. That is it should look something like this in the output: <a href="http://www.blahblah.com/1651561">Link 1</a><br> <a href="http://www.etc.com/34324">Link 2</a><br> <a href="http://www.sdfkjsdf.com/12343423">Link 3</a><br> I have learned how to read a text file with php so far but I have no idea how to make it read up to a point and assign the read value to an array. Any help would be greatly appreciated. Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/54588-read-data-from-txt-file-help/ Share on other sites More sharing options...
rusbb Posted June 7, 2007 Author Share Posted June 7, 2007 bump Quote Link to comment https://forums.phpfreaks.com/topic/54588-read-data-from-txt-file-help/#findComment-269958 Share on other sites More sharing options...
per1os Posted June 7, 2007 Share Posted June 7, 2007 <?php $file = file('link.txt'); $links = ""; foreach ($file as $line) { list($name, $link) = split(" http://", $line); $links .= '<a href="http://' . $link . '">' . $name . '</a>'; } echo $links; ?> It can probably be done better with www.php.net/preg functions, but this works also. Quote Link to comment https://forums.phpfreaks.com/topic/54588-read-data-from-txt-file-help/#findComment-269974 Share on other sites More sharing options...
rusbb Posted June 7, 2007 Author Share Posted June 7, 2007 Hi thanks for the fast reply, when I try your code however I get the following error: Warning: Invalid argument supplied for foreach() Quote Link to comment https://forums.phpfreaks.com/topic/54588-read-data-from-txt-file-help/#findComment-269980 Share on other sites More sharing options...
per1os Posted June 7, 2007 Share Posted June 7, 2007 <?php $file = file('link.txt') or die('Some error happened include the file.'); // be sure to change the link.txt $links = ""; if (is_array($file)) { foreach ($file as $line) { list($name, $link) = split(" http://", $line); $links .= '<a href="http://' . $link . '">' . $name . '</a>'; } echo $links; }else { echo 'Guess what, there was either no file, no data or the data was not on separate lines.'; } ?> Make sure you change the link.txt to the actual file, also make sure that the data is as you posted and seperated out in lines IE: Link1 Link2 As the script needs it to be on new lines due to the www.php.net/file function. Quote Link to comment https://forums.phpfreaks.com/topic/54588-read-data-from-txt-file-help/#findComment-269983 Share on other sites More sharing options...
rusbb Posted June 7, 2007 Author Share Posted June 7, 2007 wow, thats cool it works, however i have encountered another problem, I am getting the links from a file named the same as one of the items in my db and to do so i have tried to do this: $file = file('uploads/texts/{$row_pro[0]['pname']}.txt') or die('Some error happened include the file.'); // be sure to change the link.txt $links = ""; if (is_array($file)) { foreach ($file as $line) { list($name, $link) = split(" http://", $line); $links .= '<a href="http://' . $link . '">' . $name . '</a>'; } echo $links; }else { echo 'Guess what, there was either no file, no data or the data was not on separate lines.'; } where {$row_pro[0]['pname']} is the name of the database entry (worked for normal echo stuff e.g. $links = "uploads/texts/{$row_pro[0]['pname']}.txt"; $fh = fopen($links, 'r'); and so on to echo) but when i insert {$row_pro[0]['pname']} into your code it says Parse error: syntax error, unexpected T_STRING What am I doing wrong? p.s. i need it to read the file name from db because there will be a lot of these different files, different one for every different page Quote Link to comment https://forums.phpfreaks.com/topic/54588-read-data-from-txt-file-help/#findComment-269990 Share on other sites More sharing options...
per1os Posted June 7, 2007 Share Posted June 7, 2007 $file = file('uploads/texts/' . $row_pro[0]['pname'] . '.txt') or die('Some error happened include the file.'); // be sure to change the link.txt Single quotes either do not understand the { } or do not like the ' ' in the array index and should be " " . Either contact it as seen above or change the single to double. Quote Link to comment https://forums.phpfreaks.com/topic/54588-read-data-from-txt-file-help/#findComment-269996 Share on other sites More sharing options...
rusbb Posted June 7, 2007 Author Share Posted June 7, 2007 you are the best! hehehe thanks for helping me so much!! one last thing, i changed it a little: $file = file('uploads/texts/' . $row_pro[0]['pname'] . '.txt'); if (file_exists($file)) { $links = ""; foreach ($file as $line) { list($name, $link) = split(" http://", $line); $links .= '<a href="http://' . $link . '">' . $name . '</a><br>'; } $dlinks .= $links; else { $dlinks .= <<<HTML <br/><b>Links are not ready (or have been removed), come back another time</b> HTML; } } but i am getting this error: Parse error: syntax error, unexpected T_ELSE where did i go wrong in the syntax....thanks in advance again!!! you guys are very very helpful Quote Link to comment https://forums.phpfreaks.com/topic/54588-read-data-from-txt-file-help/#findComment-270011 Share on other sites More sharing options...
per1os Posted June 7, 2007 Share Posted June 7, 2007 <?php if (file_exists($file)) { $links = ""; foreach ($file as $line) { list($name, $link) = split(" http://", $line); $links .= '' . $name . ' '; } // close for $dlinks .= $links; }else { // close if $dlinks .= <<<HTML Links are not ready (or have been removed), come back another time HTML; } // close else Quote Link to comment https://forums.phpfreaks.com/topic/54588-read-data-from-txt-file-help/#findComment-270016 Share on other sites More sharing options...
rusbb Posted June 7, 2007 Author Share Posted June 7, 2007 thanks a million, you are so patient with me (i have limited php knowledge as i am sure you can tell). Somehow when i paste the whole code: $dlinksFile = file('uploads/texts/' . $row_pro[0]['pname'] . '.txt'); if (file_exists($dlinksFile)) { $links = ""; foreach ($file as $line) { list($name, $link) = split(" http://", $line); $links .= '<a href="http://' . $link . '">' . $name . '</a><br>'; } $dlinks .= $links; } else { $dlinks .= <<<HTML <br/><b>Links are not ready (or have been removed), come back another time</b> HTML; } into my php file i get this error: Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING which falls under this line: $tplt = @file_get_contents(ROOT_DIR.'/templates/'.$config['skin'].'/portfolio.tpl'); which has nothing to do with what I was doing, as soon as i remove the links code it works... any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/54588-read-data-from-txt-file-help/#findComment-270025 Share on other sites More sharing options...
per1os Posted June 7, 2007 Share Posted June 7, 2007 Need to see more code, as I do not even see that line in the code above. Quote Link to comment https://forums.phpfreaks.com/topic/54588-read-data-from-txt-file-help/#findComment-270034 Share on other sites More sharing options...
rusbb Posted June 7, 2007 Author Share Posted June 7, 2007 here is the code for the whole php file: <?php /** * My module (portfolio.php) * */ // vim: expandtab softtabstop=4 tabstop=4 shiftwidth=4: // style coding K&R if(!defined('DATALIFEENGINE')) { die("This file cannot be acessed directly!"); } // required files require_once(ENGINE_DIR.'/data/config.pf.php'); require_once(ENGINE_DIR.'/modules/portfolio.func.php'); /******************************************** * Main variables * ********************************************/ // locale $table_bd1 = PREFIX."_proekts_cat"; $table_bd2 = PREFIX."_proekts"; // cache file $file_cache = "portfolio_cat"; // path to images $path = $config['http_home_url']; // content variable $content = ''; // cell variable $add_cell = ''; // cell variable 2 $add_cell_pro =''; // category menu variable $cat_menu = ''; // module info variable $modul_info = ''; // link variable $link = ''; // modrewrite variable $chpu = $config_pf['chpu']; // get current page $page = intval(@$_GET['page']); // get category numbers $row_cat = get_cache_cat($file_cache, $table_bd1); $get_count_cat = count($row_cat); // get project numbers $row_pro = $db -> super_query("SELECT id FROM {$table_bd2}", TRUE); $get_count_pro = (gettype($row_pro) == "array") ? count($row_pro) : 0; /***************************** * Category display * *****************************/ // if there is at least one category show it if ($get_count_cat > 0) { $cat_menu .= '<table width="100%" height="100%" class="cat_navigation"><tr>'; // number of category rows $c_tr = 0; // show category list for ($i = 0; $i < $get_count_cat; $i++) { // Sort into 9 columns if (($i % 9 == && ($i != 0)) { $tr1 = ''; $tr2 = '</tr><tr>'; $c_tr++; } else { $tr1 = ''; $tr2 = ''; } // modrewrite support if($chpu == 1) { $linkc = "<a href=\"{$path}portfolio/cat_{$row_cat[$i]['catnamelat']}\">"; } else { $linkc = "<a href=\"{$path}index.php?do=portfolio&cat={$row_cat[$i]['catnamelat']}\">"; } $cat_menu .= $tr1. '<td style="padding:3px; padding-left: 10px; border: 1px solid #F0F0F0;"> <img src="{THEME}/images/pf/pointer.gif" width="15" height="15" /> '.$linkc . $row_cat[$i]['catname'].'</a></td>'.$tr2; } // add blank cells if neede if ($get_count_cat % 3 == 0) { $add_cell = ''; } else { for ($k = 0; $k < ((3 * ($c_tr + 1)) - $get_count_cat); $k++) $add_cell .= '<td style="padding:3px; padding-left: 10px; border: 1px solid #F0F0F0"> </td>'; } $cat_menu .= $add_cell.'</tr></table>'; // if no categories available display this message } else { $cat_menu = '<div id="fh3"> - There are currently no categories - </div>'; } // Information block $modul_info .=<<<HTML <table width="100%" height="100%"> <tr> <td class="pf_info" style="padding-left:20px; border-bottom:1px solid #F0F0F0;"> <b>Statistics </b></td></tr> <tr> <td class="pf_info"> <img src="{THEME}/images/pf/pointer1.gif" width="13" height="9" /> Categories: {$get_count_cat} </td></tr> <tr> <td class="pf_info"> <img src="{THEME}/images/pf/pointer1.gif" width="13" height="9" /> Titles: {$get_count_pro} </td></tr> </table> HTML; ////////////////////////////////////////////////////// // If no category chosen, show latest titles if (($_SERVER['QUERY_STRING'] == 'do=portfolio') || !empty($_REQUEST['page'])) { // checking number of titles $get_query_pro = "SELECT * FROM {$table_bd1}, {$table_bd2} WHERE {$table_bd1}.`id` = {$table_bd2}.`pcat` ORDER BY {$table_bd2}.`pname` ASC"; $row_pro = $db -> super_query($get_query_pro, TRUE); $get_count_pro = (gettype($row_pro) == "array") ? count($row_pro) : 0; $not_pro = 1; ////////////////////////////////////////////////////// // If category chosen, show category titles } elseif (!empty($_REQUEST['cat']) && empty($_REQUEST['id'])) { // checking category $catlat = strip_cat ($_REQUEST['cat']); // show quotes if no category chosen if(!get_magic_quotes_gpc()) { $catlat = mysql_escape_string($catlat); } // checking number of titles $get_query_pro = "SELECT * FROM {$table_bd1}, {$table_bd2} WHERE {$table_bd1}.`id` = {$table_bd2}.`pcat` AND {$table_bd1}.`catnamelat` = \"$catlat\" ORDER BY {$table_bd2}.`pname` ASC"; $row_pro = $db -> super_query($get_query_pro, TRUE); $get_count_pro = (gettype($row_pro) == "array") ? count($row_pro) : 0; ////////////////////////////////////////////////////// // Еif category and title chosen show the title } elseif (!empty($_REQUEST['cat']) && !empty($_REQUEST['id'])) { // checkign category $catlat = strip_cat ($_REQUEST['cat']); // checking title $pro_id = strip_cat ($_REQUEST['id']); // showing quotes if no category or title chosen if(!get_magic_quotes_gpc()) { $catlat = mysql_escape_string($catlat); $pro_id = mysql_escape_string($pro_id); } // checking number of titles $get_query_pro = "SELECT * FROM {$table_bd1}, {$table_bd2} WHERE {$table_bd1}.`id` = {$table_bd2}.`pcat` AND {$table_bd1}.`catnamelat` = \"$catlat\" AND {$table_bd2}.`id` = \"$pro_id\" ORDER BY {$table_bd2}.`pname` ASC"; $row_pro = $db -> super_query($get_query_pro, TRUE); $get_count_pro = (gettype($row_pro) == "array") ? count($row_pro) : 0; } ////////////////////////////////////////////////////// // Page navigation // How many titles on main page if(!empty($config_pf['pro_on_main']) && ($config_pf['pro_on_main'] <= $get_count_pro)) { $pro_on_main = $config_pf['pro_on_main']; } elseif (empty($config_pf['pro_on_main'])) { $pro_on_main = 6; } else { $pro_on_main = $get_count_pro; } // choose which number to start showing titles from $start = $page * $pro_on_main - $pro_on_main; $total = @ceil(($get_count_pro / $pro_on_main)); // check what was received from db if ($page <= 0 || $total == 0 || $get_count_pro == 0 || $start > $get_count_pro) { $page = 1; $start = 0; } // extra navigation parameters if(($pro_on_main * $total >= $get_count_pro) && ($page == $total)) { $add = $pro_on_main - ($pro_on_main * $total - $get_count_pro); } else { $add = $pro_on_main; } /*************************************** * Title display * ***************************************/ $dlinksFile = file('uploads/texts/' . $row_pro[0]['pname'] . '.txt'); if (file_exists($dlinksFile)) { $links = ""; foreach ($file as $line) { list($name, $link) = split(" http://", $line); $links .= '<a href="http://' . $link . '">' . $name . '</a><br>'; } $dlinks .= $links; } else { $dlinks .= <<<HTML <br/><b>Links are not ready (or have been removed), come back another time</b> HTML; } ////////////////////////////////////////////////////// // If at least one title exists show it // Number of title rows $c_tr_pro = 0; if ($get_count_pro > 0) { // if id chosen, take to that project if (!empty($pro_id)) { // create category menu $menu_cat = trans($row_cat, $row_pro[0]['pcat']); // get methods $mass_tools = explode('|', $row_pro[0]['checktools']); $tplt = @file_get_contents(ROOT_DIR.'/templates/'.$config['skin'].'/portfolio.tpl'); $check1 = (!empty($mass_tools[0])) ? 'on' : 'off'; $tplt = str_replace("{check1}", $check1, $tplt); $check2 = (!empty($mass_tools[1])) ? 'on' : 'off'; $tplt = str_replace("{check2}", $check2, $tplt); $check3 = (!empty($mass_tools[2])) ? 'on' : 'off'; $tplt = str_replace("{check3}", $check3, $tplt); $tplt = str_replace("{proekt_name}", $row_pro[0]['pname'], $tplt); $tplt = str_replace("{cat_name}", $menu_cat, $tplt); $tplt = str_replace("{id}", $row_pro[0]['id'], $tplt); $tplt = str_replace("{short desc}", $row_pro[0]['plink'], $tplt); $tplt = str_replace("{date}", $row_pro[0]['pdate'], $tplt); $tplt = str_replace("{image}", $row_pro[0]['image'], $tplt); $tplt = str_replace("{long desc}", $row_pro[0]['pdetail'], $tplt); $tplt = str_replace("{firma}", $row_pro[0]['pfirma'], $tplt); $tplt = str_replace("{dlinks}", $dlinks, $tplt); $con .= $tplt; // Else show list of titles } else { // Number of rows for titles $c_tr_pro = 0; // MVC model for ($p = $start; $p < $start + $add ; $p++) { // Вывод по 2 в столбец if (($p % 2 == 1) && ($p != 0)) { $tr3 = ''; $tr4 = '</tr><tr>'; $c_tr_pro++; } else { $tr3 = ''; $tr4 = ''; } // if there is no latin name for category get it from fb if($not_pro == 1) { $catlat = $row_pro[$p]['catnamelat']; } // modrewrite support if($chpu == 1) { $link = "<a href=\"{$path}portfolio/cat_{$catlat}/diz{$row_pro[$p]['id']}.html\">"; } else { $link = "<a href=\"{$path}index.php?do=portfolio&cat={$catlat}&id={$row_pro[$p]['id']}\">"; } $content .= " {$tr3} <td width=\"120\" style=\"padding:20px;\" align=\"center\"> $link <img src=\"{$path}uploads/proekts/mini/{$row_pro[$p]['image']}\" width=\"80\" height=\"80\" class=\"min_im\"/></a></td> <td width=\"300\" class=\"description\"> <img src=\"{THEME}/images/pf/arow.gif\" width=\"3\" height=\"6\" align=\"absmiddle\" /> <a href=\"{$path}index.php?do=portfolio&cat={$catlat}&id={$row_pro[$p]['id']}\" title=\"{$row_pro[$p]['pname']}\"> {$row_pro[$p]['pname']} </a> <br /> {$row_pro[$p]['plink']} </td> {$tr4}"; } // add blank cells if needed if ($pro_on_main % 2 == 0) { $add_cell_pro = ''; } else { for ($z = 0; $z < ($pro_on_main - (2 * $c_tr_pro)); $z++) $add_cell_pro .= '<td width=\"120\" style=\"padding:10px;\"> </td> <td width=\"300\" class=\"description\"> </td>'; } $content .= $add_cell_pro; } // Navigation block if(!empty($_REQUEST['cat'])) $catnav = $_REQUEST['cat']; $navigation = navigation($catnav); $con .= <<<HTML {$content} </tr><tr> <td colspan="4" style="text-align:center;"> {$navigation} </td> HTML; // If there is no titles diplay the following message } elseif ($get_count_pro <= 0 && $not_pro == 0) { $con .= '<td><div id="fh3"> - There are no titles in this category - </div></td>'; // If there is no title under a chosen id show this message } elseif (!empty($pro_id) && $not_pro == 0) { $con .= '<td><div id="fh3"> - Title does not exist, must have been deleted - </div></td>'; // If there no titles } else { $con .= '<td><div id="fh3"> - There is currently no titles - </div></td>'; } /******************************** * template block * ********************************/ $tpl -> load_template('portfolio_all.tpl'); $tpl -> set("{MENU_CATEGORY}", $cat_menu); $tpl -> set("{INFO_MODUL}", $modul_info); $tpl -> set("{CONTENT}", $con); $tpl -> set("{PATH}", $path); $tpl -> compile('content'); $tpl -> clear(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/54588-read-data-from-txt-file-help/#findComment-270042 Share on other sites More sharing options...
per1os Posted June 7, 2007 Share Posted June 7, 2007 No clue, I never use the <<<HTML HTML; those type of tags, but I think that is where the error is as in the syntax highlight shown that is where it gets screwed up. else { $dlinks .= <<<HTML <br/><b>Links are not ready (or have been removed), come back another time</b> HTML; } Right there, also I wouldn't use file_exists, I would use is_array to check right here if (file_exists($dlinksFile)) { Quote Link to comment https://forums.phpfreaks.com/topic/54588-read-data-from-txt-file-help/#findComment-270051 Share on other sites More sharing options...
rusbb Posted June 7, 2007 Author Share Posted June 7, 2007 Okay my problem has been solved, i thank you countless times.. you are a php god this is the final working code $dlinksFile = file('uploads/texts/' . $row_pro[0]['pname'] . '.txt'); if (is_array($dlinksFile)) { $links = ""; foreach ($dlinksFile as $line) { list($name, $link) = split(" http://", $line); $links .= '<a href="http://' . $link . '">' . $name . '</a><br>'; } $dlinks .= $links; } else { $dlinks .= '<br/><b>Sorry - the links are not ready (or have been removed), come back another time</b>'; } Quote Link to comment https://forums.phpfreaks.com/topic/54588-read-data-from-txt-file-help/#findComment-270060 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.