Click to See Complete Forum and Search --> : Php beginner's question


Archimedes
08-13-2007, 04:57 AM
I'm new in Php and need help with following section of my script. Thanks.
nameyl:

1. to create a function that reads a file into Array $arParsedLines..
2. mark all lines inside the array $arParsedLines with active=false
3. Walk though Array $arLineNumbersor and for each Line-Number in that array set active=true for the corrosponding line the Array $arParsedLines
Thanks.



//read the page.conf here into Array $arParsedLines

function read_contents()
{
$arParsedLines = fread($nFileHandle)
}
//mark all lines inside $arParsedLines with active=false

{
foreach (arParsedLines as &$value)
{
$value = $value["active"]==false
array_walk($arLineNumbers);
echo " arLineNumbers["active"]==true
}

//walk through the Array $arLineNumbers
//for each Line-Number in that array:
//set active=true for the corrosponding line the Array $arParsedLines

hastx
08-13-2007, 01:18 PM
I'm not sure I understand what your goal is, but to get the lines of a file into an array you would use file().


$arParsedLines = file('page.conf');


Start there, it may help.

Maximus9999
08-13-2007, 01:42 PM
Yes i'm also unsure on what u'r trying to do. It sounds like you want two keys "active" and "line". Based on the code given, I've helped you read each line into your array. You will need to change the array if you want to add 'active'.



$filehandler = fopen('test.txt', 'r') or die($php_errormsg);
$lineNumbers = read_contents($filehandler);

function read_contents($filehandler)
{
$arLineNumbers=array();

// read 256 bytes into each line
while(!feof($filehandler))
{
$arLineNumbers[$counter] = fgets($filehandler,256);
$counter++;
}
fclose($filehandler);

return $arLineNumbers;
}

foreach($lineNumbers as $value)
{
echo $value."<br />"; // test
}

hastx
08-13-2007, 02:21 PM
Is this just an exercise for school or something?

Maximus9999
08-13-2007, 02:24 PM
No, why?

hastx
08-13-2007, 03:58 PM
I meant the original Post. I didn't understand the end goal, It just seems like the kind of problem you would get in a class.

Maximus9999
08-13-2007, 04:42 PM
oh, haha i misunderstood sorry.

i know what you mean.

Archimedes
08-14-2007, 02:34 AM
No, it's not a School exercise of some sort but my first php micro project.
The script I sent was a part of php file display.php that is supposed to process a html form showpage.html. Thanks.

Here is Showpage.html

<html>
<head>
</head>
<body>
<?php

//List for storing the parsed lines
$arParsedLines = array();

//read file
$nFileHandle = fopen("paga.conf", "r+");

while (!feof($nFileHandle))
{
//echo "--------------------------------------------------------------------<br>\n";
$strCurrentLine = fgets ( $nFileHandle, 4096 );

if (strlen($strCurrentLine) < 7)
{
continue;
}//end if (strlen($strCurrentLine) < 7)

//echo "strCurrentLine enthält: ->".$strCurrentLine."<-<br>\n";

//echo "Datenstruktur wird angelegt: <br>\n";
$arThisLine = array("url" => "", "email" => "", "size" => "", "command" => "", "active" => true);

//echo "Datenstruktur arThisLine: <br>\n";
//print_r($arThisLine);

//echo "\n<br>suche nach hash<br>\n";
$nPosOfHash = strpos($strCurrentLine, "#");

if (!($nPosOfHash === false))
{
//echo "HASH gefunden: Position: ".$nPosOfHash."<br>\n";
$arThisLine["active"] = false;

$strCurrentLine = substr ( $strCurrentLine, 1);

}//end if (!($nPosOfHash === false))
else
{
//echo "Kein HASH in der aktuellen Logzeile gefunden<br>\n";
}//end else (!($nPosOfHash === false))

//echo "strCurrentLine ist jetzt: ->".$strCurrentLine."<-<br>\n";

$arTemp = explode("\t", $strCurrentLine);

//echo "Ergebnis von Explode: <br>\n";
//print_r($arTemp);
//echo "\n<br>ende<br>\n";

$arThisLine["url"] = $arTemp[0];
$arThisLine["email"] = $arTemp[1];
$arThisLine["size"] = $arTemp[2];
$arThisLine["command"] = $arTemp[3];

//echo "Datenstruktur arThisLine: <br>\n";
//print_r($arThisLine);
//echo "\n<br>ende<br>\n";

array_push($arParsedLines,$arThisLine);

}//end while (!feof($nFileHandle))


//echo "angesammelte Liste:<br>\n";
//print_r($arParsedLines);
?>

<form name="urllist" action="Display.php" method="get">
<table border=1>
<tr>
<th></th>
<th></th>
<th>URL</th>
<th>Notify-Email</th>
<th>Size</th>
<th>Command</th>
</tr>

<?php
foreach($arParsedLines as $key => $arLine)
{
echo "<tr>\n";
echo " <td></td>\n";
echo " <td><input type=\"checkbox\" name=\"active_".$key."\" value=\"active\"";
if ($arLine["active"]==true)
{
echo "checked";
}
echo "></td>\n";
echo " <td>".$arLine["url"]."</td>\n";
echo " <td>".$arLine["email"]."</td>\n";
echo " <td>".$arLine["size"]."</td>\n";
echo " <td>".$arLine["command"]."</td>\n";
echo "</tr>\n";
}//end foreach($arParsedLines as $arLine)
?>

</table>
<input type="submit" name="uebernehmen" value="uebernehmen">
</form>
</body>
</html>



And now Display.php


<html>
<head>
<title>Display</title>
<body>
<p>Display Active Checkboxes<p>
<pre>
Content of Array $_GET:
<?php
//get available Keys out of array

$arOurKeys = array_keys($_GET);

//for each key that matches "active_<number>"
//we want to do something

/*
$active = $_GET['active_.$key.'];
if($arLine["active"]==true)
{
echo $active.": ".$_GET[$active]."<br>\n";
}
*/

print_r($_GET);

echo "our keys:\n";
print_r($arOurKeys);


/*
$nArrayLength = count($arOurKeys);
for ($i=0; $i<$nArrayLength; $i++)
{
echo "Loop Variable i is: ".$i."\n";
echo $arOurKeys[$i];
echo "\n";
}//end for ($i=0; $i<$nArrayLength; $i++)
*/

$arLineNumbers = array();

foreach($arOurKeys as $strArrayElement)
{
echo "Value of strArrayElement is: ".$strArrayElement." ";
if (strncmp($strArrayElement, "active_", 7 ) == 0)
{
echo "we have a match ";
$strNumber = str_replace ( "active_", "", $strArrayElement);
echo "extracted Line-Number: ".$strNumber."\n";
array_push($arLineNumbers, $strNumber);
}//end if (strncmp($strArrayElement, "active_", 7 ) == 0)
}//end foreach($arOurKeys as $strKey)

echo "content of LineNumbers:\n";
print_r($arLineNumbers);

//read the page.conf here into Array $arParsedLines

$filehandler = fopen('paga.config', 'r');
$lineNumbers = read_contents($filehandle);


function read_contents($filehandle )
{

$arLineNumbers=array();
// read into each line
while(!feof($filehandle))
{
$arLineNumbers[$counter] = fgets($filehandle);
$counter++;
}
fclose($filehandle);

return $arLineNumbers;
}
foreach($LineNumbers as $value)
{
echo $value."<br />"; // test
}

//mark all lines inside $arParsedLines with active=false

{
foreach (arParsedLines as $value)
{
$value = $value["active"]==false
array_walk($arLineNumbers);
echo " arLineNumbers["active"]==true
}

?>
</body>
</html>