View Full Version : PHP programming question
does anyone here know anything about PHP?
I am trying to figure out how in the heck this code can read a file and print something out?
but I only know enough to get myself into trouble...
anyway, here is what I am talking about if anyone can help me.
$url = ($settings['clean'] == 0) ? $url : 'go.php?url='.rawurlencode($url);
$nofollow = $settings['use_nofollow'] ? 'rel="nofollow"' : '';
echo '
<tr>
'.$thumb_code.'
'.$pr_code.'
<td valign="top" class="linkman"><p class="linkman"><a href="'.$url.'" target="_blank" class="linkman" '.$nofollow.'>'.$title.'</a>'.$show_url.'<br>'.$description.'
Hobbyist
08-19-2010, 07:04 AM
hmmm.. I know enough to get myself in trouble as well.. what makes you think that's what it does? Here's how I read this.. it's I M H O !!!!!
The first line says if the array called $settings has a value of 0 assigned to the element called 'clean' then don't do anything to the value of the variable $url (ie just reassign it back to $url) otherwise the '.$url' reference in the A tag (line 7) will become 'go.php?url=' followed by a "cleaned up" version of the current value of $url (ie cleaned up by the rawurlencode function). In other words the target of your A tag will be _either_ the value of url _or_ go.php with the value of url passed as a parameter
The second line sets another value for the A tag. It says if the array called $settings has a value (remember anything non 0 == true) then $nofollow gets the string rel="nofollow" otherwise it gets an empty string (those are 2 single quotes - not a single double quote). This just sets an optional rel= reference for the A tag.
The rest puts out the html that will genereate the first data element in a row in the "current table" on the page when it's pulled by the user's browser.,,
' < tr > '
now concat the values of $thumbcode and $pr_code. In my opinion this is an odd place to put "normal" text but either or both of these 2 variables could contain many lines of text.
$thumb_code
$pr_code
then a data tag then a paragraph tag and open that A tag and specify the target
' < td valign="top" class="linkman" >
<p class="linkman>
<a href=" '
now here's the concat of the (optional) call to go.php when the user clicks the link (ie $title) specified in the A tag
$url
concat the rest of the A tag components
' " target="_blank" class="linkman" '
now here's the concat of the rel (or empty string) set up in line 2
$nofollow
concat the closing angle braket of the A tag
'>'
concat the text for the link from $title
$title
concat the close A tag
' < /a > '
Here's where you dump everything that the $show_url variable has in it.. maybe this is where all your data comes from.
$show_url
After that concat a page break
'< br >'
Now dump everything that the variable $description has in it.. this could have a ton of lines of text, etc as well. If I were looking for where all the lines are coming from I'd use the view source choice in my browser and see if that break tag appears right before the lines.
$description
There's still a missing < /td > and a missing < /tr > to end the data element and row tags
HTH
Webscout
08-20-2010, 04:27 PM
...did you get that Elmer.
I got this part......'Now dump everything ....:D
I got most of it. :)
thanks, Hobbyist
I added the break tag because I didn't know if it was actually getting to that section of code or not. when I added the break tag it read the text file and printed each line of the text file with the break in between.
so it has to have some kind of loop that keeps it going through the file???
I have kind of given up trying to figure this one out, but now I have another question.
hopefully this one is easier.
ok, here is the code
$link=true;
if (file_exists('inkm.php')) {include_once('inkm.php');
if (@is_array($settings['inkm'])) {$link=false;}}what I need to know is exactly what kind of file do I need for inkm.php
or in other words if I was going to create this file with notepad, what would it need to have in it?
Hobbyist
08-25-2010, 07:41 AM
Sounds like your original post was from a function (ie subroutine) that was being called from somewhere else repeatedly (ie once for each line it was supposed to print).
I would indent this code fragment a little differently :
$link=true;
if (file_exists('inkm.php')) {
include_once('inkm.php');
if (@is_array($settings['inkm'])) {
$link=false;
}
}
The variable $link is used to track if the second if-statement was reached and evaluated to true. It's initially set it to true and then after this whole section of code executes it will be false iff the first if-statement evaluated to true AND then the second if-statement also evaluated to true.
The first if-statement just makes sure the inkm.php file is "available". It's likelythere just to prevent a "crash and die" if the file can't be found.
The third line (the way I indented) says include the contents of inkm.php at that point. With php an include is exactly like you typed the lines in that file at the point where the include statement appears. The include_once is usually used to include files that contain function definitions, etc where you don't want to accidently define the same functions more than once. inkp.php can contain any php statements you like. From the context here it is impossible to tell what would be in that file. If you put
echo '<b>Hi Elmer</b><br>';
then executing the lines you posted would put Hi Elmer in bold followed by a page break at the point on the page where this routine was executed (provided the file was found).
It would be like
File 1 (main file lets say index.php)
<html>
<head></head>
<body>
<b>Who's our favorite dude?</b>
<?php
include( dude.php );
?>
<b>He's the best</b>
</body>
</html>
And then create a file dude.php that contained the lines:
echo '<h1>'
echo '<b>Elmer</b>'
echo '<h2>' . date('l jS \of F Y h:i:s A') . '</h2>';
echo '</h1>'
when the user loads the index.php file what his browser would get is
<head></head>
<body>
<b>Who's our favorite dude?</b>
<h1>
<b>Elmer</b>
<h2>Monday 24th of August 2010 09:12:46 AM</h2>
</h1>
<b>He's the best</b>
</body>
</html>
The second if statement uses the "error control operator" the @ symbol (see http://ca3.php.net/manual/en/language.operators.errorcontrol.php). It just prevents an error from producing any output. In this case it's there in case the $settings array doesn't have a "position" called inkm defined at all. If it is defined then if it is itself an array (ie name1=value1, name2=value2, etc) then set your "test flag" $link to false (otherwise it will remain true).
So.. with the code you posted - there is no way to know what php statements are in that inkm.php file that gets included here.. It likely includes function definitions, etc (I'm guessing) because it is only going to be included once even if it gets mentioned again somewhere else.
As always I M H O!!!!
HTH
ok, I created a file with note pad and called it "inkm.php"
then I just put 15 lines of numbers in it,
I figured it could read them as numbers or characters,
anyway, it worked.
but back to the original code section,
the line above the code I have shown,
is a print statement that only displays one time, it's a header line,
so it seems that code has to loop someplace since the break I added gets displayed one time for each line in the file,
I don't know if PHP has some kind of goto statement that will jump into the middle of a block of code or not?
but I didn't see anything.
but I am pretty much decided that one is over my head, because I want to add another variable to the text file it reads then only print some of the lines depending on the variable,
but I think I am too far away on that one since I can't even figure out who it works at all.
Hobbyist
08-25-2010, 11:47 AM
The tutorial at php.com (ie http://ca3.php.net/tut.php ) is a good (and fairly short) intro tutorial..
Normally I'd think code that needed to be repeated would be inside a function definition and then a looping structure like for/next, while/do, etc can make calls to the whole block by refering to the function name. I suspect your first code fragment was inside the function that prints a single line (that would explain why changes there affect every line). I'm not clear on whats up with the header line.
I would first identify the variable that holds the line of text you want to modify. That $description one would be my guess from what you've said about what you want to do. I would go back to the first code and insert a new line 3 (in the blank space before the current line 3)
echo '<br><br><br> description contains <br><br> ' . $description . ' <br><br><br>';
If that isolates the exact blocks of text you want to fool around with, you're laughing. You could then just use the php string functions to search $descrition for the position of a substring that marks the start of where you want to change, copy the preceeding characters to a temporary variable, append the charters you want to add, append the rest of whatever comes after the insert point in $description, then assign the new value back to $description (ie equal to the string you built in the temporary variable).
HTH
Update -> I've re-read your post above.. you're cutting things a bit too short for me to have a very good idea where the looping happens.. include a few more (preceeding) lines perhaps..
ok, right before the original code it looks like this...
<p><b>Website description:</b><br>
<input type="text" name="description" maxlength="200" size="60"></p>
<p><input type="submit" value="Add link"></p>
</form>
<?php
} // End if $settings['max_links'] < $i
else
{
?>
<p class="linkman"> <br /><b>Submit your website</b></p>
<p><i>Unfortunately we are not accepting any new links at the moment.</i></p>
<?php
}
} // End if $settings['show_form']
the phrase "Website description:"
gets displayed only one time.
the variables $title.'</a>'.$show_url.'<br>'.$description.'
get displayed one time for each line in the file that is being read.
I know this because I added the <br> and when I did it showed up on each line of the file that was displayed. there were 4 records in the file so 4 breaks showed up.
but like I said I am about ready to give up on this one.
thanks for your help.
Hobbyist
08-25-2010, 04:42 PM
I think you are being confused by the way php and html intermix.. the code here shows the author wants to have one of two different blocks of html reach the users browser. The first defines an html form that end with
<p><b>Website description:</b><br>
<input type="text" name="description" maxlength="200" size="60"></p>
<p><input type="submit" value="Add link"></p>
</form>
the other possibility is
<p class="linkman"> <br /><b>Submit your website</b></p>
<p><i>Unfortunately we are not accepting any new links at the moment.</i></p>
So.. the user _either_ gets a form which allows him to submit some info _or_ gets an apology that no info is being accepted.
The logic you posted will have started with something like
<?php
if (???) {
?>
then the first possible block of html statements
<?php
} else {
?>
then the second block of html statements
<?php
};
?>
Notice that dropping in and out of php does not "break" the logic of the if statement allowing 2 different possible blocks of html to be generated based on what ever is in the if part. From the comment "// End if $settings['max_links'] < $i" I suspect the if statement reads
if ( $settings['max_links'] < $i )
which shows the variable $i carrys the number of elements (and the $settings array has a slot max_links with a value equal the "max number of links").
There is another clue that this whole structure is included in a larger if-statement because of the extra close brace and the comment "// End if $settings['show_form']". The $settings array also apparently has a slot 'show_form' that would likely disable the generation of either (ie both) html block(s).
There will likely be a variable (starting with $) that will have a whole array of "lines". If you mentioned reading a file this may have been done and assigned to an array at this point. You will likely be getting a heading line and then an array of data lines in the function that displays everything. The array itteration commands get kind of obscur and criptic if I remember right. Maybe the looping you are talking about is hiding in a few little characters that you haven't posted yet... I'd go looking for anything
... $something[ $i ] ...
that will be an array of elements (ie links, lines, etc)
HTH
well I have a bit of an advantage because I have seen the output,
but I think the key factors are that it displays
"Website description:" only one time, and it never displays
"Submit your website" so it can't really come down through those statement multiple times without displaying one of those, and that is in the form anyway, and the form displays only 1 time.
then after website description it displays each lines of the text file that it read.
and there is more code above and below it, but I am pretty sure it's not doing anything to this section.
vBulletin® v3.6.8, Copyright ©2000-2012, Jelsoft Enterprises Ltd.