PHP - Somone Explain This Code?
Code: [Select]
if ($r['parent_id'] > 0) { $this->children[ $r['parent_id'] ][$r['id']] = $r; } else { $this->forums[ $r['id'] ] = $r; } what is this code doing? i don't get it how it uses the 2 variables in the " [ " " ] " 's ? weird? $r is a simple select * from a query.. i dont get the 2 variables with [] right next to each other? Similar TutorialsHi everyone! I'm making friends with DOM+PHP+XML. I found an example in w3school tutorial but there are some things I don't understand. Code: [Select] <?php $q=$_GET["q"]; $xmlDoc = new DOMDocument(); $xmlDoc->load("cd_catalog.xml"); $x=$xmlDoc->getElementsByTagName('ARTIST'); for ($i=0; $i<=$x->length-1; $i++) { //Process only element nodes if ($x->item($i)->nodeType==1) { if ($x->item($i)->childNodes->item(0)->nodeValue == $q) { $y=($x->item($i)->parentNode); } } } $cd=($y->childNodes); for ($i=0;$i<$cd->length;$i++) { //Process only element nodes if ($cd->item($i)->nodeType==1) { echo("<b>" . $cd->item($i)->nodeName . ":</b> "); echo($cd->item($i)->childNodes->item(0)->nodeValue); echo("<br />"); } } ?> This part I understand, it's a PHP method: Code: [Select] $xmlDoc = new DOMDocument(); $xmlDoc->load("cd_catalog.xml"); $x=$xmlDoc->getElementsByTagName('ARTIST'); but I don't get the rest , and what is this Code: [Select] $x->length-1 means? What does this mean? Code: [Select] nodeType==1 what do the numbers represent? Can anyone give me the URL of a good XML PHP tutorial? Thank you Hi guys, for a long time now I have been using the same block of code to include my content pages into my layout, I understand what it does as a whole, but I have never understood what the individual parts do, could someone explain the main parts please. Thanks. here's the code: Code: [Select] <?php if (isset($_GET['page'])) { if (strpos($_GET['page'], "/")) { $direc = substr(str_replace('..', '', $_GET['page']), 0, strpos($_GET['page'], "/")) . "/"; $file = substr(strrchr($_GET['page'], "/"), 1); if (file_exists($direc.$file.".php")) { require($direc.$file.".php"); } else { require("error.php"); } } else { if (file_exists(basename($_GET['page']).".php")) { require(basename($_GET['page']).".php"); } else { require("error.php"); } } } else { require("default.php"); } ?> I have some code that is supposed to get data from a site and then insert the gathered data into a database. The code does start adding data to database but it isn't adding data that it is getting from site that has starting data. What I mean is this. This code gets current player scores and such from gametracker and adds that to the database. then it will get player scores from my own tracker and add new scores to the ones that weere adde3d from game tracker. We did this so that our game servers continue tracking the scores for our new server trackers and not start out at zero initially. How ever when we run the code it says data was not written to database probably because server does not exists in database when in fact it really does. SO if someone could explain the code to me as to what it is doing then maybe I can figure out why it isn't working. Code: [Select] function InitializeServer( $sid, $ip, $name, &$totalplayers ) { !mysql_query( "DROP TABLE ".SERVER_TABLE_NAME.$sid.";" ); // we don't care if this fails if table didn't exist before if( !mysql_query( "CREATE TABLE ".SERVER_TABLE_NAME.$sid." (" ." `name` varchar(32), `score` int, `goal` int default 0, `leader` int default 0, `enemy` int default 0, `kia` int default 0, `roe` int default 0," ." PRIMARY KEY(`name`) );" ) ) { die( "Error creating table: ".mysql_error() ); } $id = 1; $page = 1; $done = FALSE; while( !$done ) { $data = http_request( "www.gametracker.com", "/server_info/".$ip."/top_players/?searchipp=50&searchpge=$page" ); $pos = 0; $out = ""; while( !$done ) { $num = GetStringInMiddle( $data, $pos, '<td class="c01">', '</td>' ); if( $num == NULL && $id > 1 ) break; if( $num != $id || $num == NULL ) { $done = TRUE; break; } $name = GetStringInMiddle( $data, $pos, '/">', '</a>' ); if( $name === NULL ) break; $score = GetStringInMiddle( $data, $pos, '<td class="c04">', '</td>' ); //$timePlayed = GetStringInMiddle( $data, $pos, '<td class="c05">', '</td>' ); //$scorePerMinute = GetStringInMiddle( $data, $pos, '<td class="c06">', '</td>' ); if( $out != "" ) $out .= ",\n"; $out .= "('$name', '$score')"; $id ++; $totalplayers ++; } // Write data to database, if any if( $out != "" ) { $cmd = "INSERT INTO ".SERVER_TABLE_NAME.$sid." (`name`, `score`) VALUES ".$out.";"; if( !mysql_query( $cmd ) ) { echo "Error with insert: ".mysql_error()."\n[$cmd]"; } } echo ($id-1)." "; flush(); ob_flush(); $page++; } if( $id > 1 ) echo "\nInitalized server #$sid: ".$ip." - ".$name." - ".($id-1)." players added<br />"; else echo "\nServer #$sid: ".$ip." - ".$name." - no players added (server probably not in database)<br />"; flush(); ob_flush(); } $sid, $ip and such are defined in other files that are included in the same file as this code is included in. i found some code that is helping me extract a table. Currently it works and extracts every single table row however it duplicates some of the rows. I am new to XPATH and would appreciate if someone could comment the following code so that I can alter it to meet my needs. Thanks. $newDom->appendChild($newDom->importNode($item,true)); $xpath = new DOMXPath( $newDom ); foreach ($item->attributes as $attribute) { for ($node = $item->firstChild; $node !== NULL; $node = $node->nextSibling) { if (($attribute->nodeName =='valign') && ($attribute->nodeValue=='top')) { print($node->nodeValue); } else { print("<br>".$node->nodeValue); } } print("<br>"); I looked up 'how to get all POST variables' and I ended up putting this bit of code together. Im not experienced at all with OOP programming, but I think that when '=>' is used its to do with OOP? Anyway. I dont get this code. I understand Foreach $_POST, as $someVar, (BTW im looking for checkboxes from a form), However, If I leave out the '=> $val' I simply get a lot of 'on' values, indicating the checkboxes I checked. However, WITH the '=> $val' I end up with the checkbox name. Now i've changed the name of '$val' so i know its not a keyword. But i dont get it. Is there a way to access the value of the checkbox? Can someone explain to me whats going on here? I know that I can get the value of the checkbox by $_POST['$checkRows'] But, I have a feeling It can be done in the foreach line. foreach($_POST as $checkRows => $hip) { echo "POSTED: $checkRows<br>\n"; } Please help! This topic has been moved to Third Party PHP Scripts. http://www.phpfreaks.com/forums/index.php?topic=330152.0 Code: [Select] $correct_numbers = 0; $correct = 0; $numbers_chosen = @explode("|",$ticket['numbers_chosed']); $your_numbers = array(); $array1 = array_count_values($numbers_chosen); $array2 = array_count_values($winning_numbers); foreach($array1 as $number1 => $count1) { foreach($array2 as $number2 => $count2) { if($number2 == $number1 and $count2==$count1) $correct_numbers += $count2; } } $use = 0; $numbers = @implode("|",$winning_numbers); echo $correct_numbers; echo "<br>"; echo $lottery['balls']; I have no idea wthell this does Code: [Select] $correct_numbers += $count2; What's that += suppose to mean? I know the $correct_numbers is supposed to be a matching array count from the for eachs right? $ticket['numbers_chosed'] is Code: [Select] 8|18|3 Ok, this is more about the method as apposed to the code but obviously related. Im currently doing it one way not sure if its the best option or not, so just need advising. I have a project where by I have multiple servers around the uk, (basic computer configured as a webhost). Each have a mysql database which holds data collected every minute. (its not alot of data, 3 rows, each a few values) Then I have a one central server here with me which has my web application hosted where the user can visit the webpage hosted on the central server and look at data from any one of the databases scattered around uk. The way in which im currently doing this is having the remote basic servers that collect data open port 3303 which allows me to directly access the SQL database from the central server, but I have a feeling thats not really how it should be done, plus it leaves an open database port available, or is it ok to do it this way? The reason I did it like this was so all code is managed here on the central server, all it is doing is collecting data directly from the servers as and when. The other option I can think of is to have a php file sit on each server that simply requests the data and saves them into an array. This file can then be INCLUDED into the webpage on the central server? So when the user logs on and wants to look at a certain servers data, it will include the remote php file which collects the data locally? This though does mean if i want to make any changes to the whole thing, I would need to somhow change all the php files that reside around the uk on the servers, not a huge deal as I can just remotly take over the servers and thansfer new files but more work nonetherless. The other way I thought would be similer to above, except dont INCLUDE it, but have the remote php file post back the data from a request. I am litterally not sure which is correct, or if it matters? Or if there are better ways Hope you help. Thanks Andy If a user is using internet explorer, display error "You are not using a browser that is supported. Please reload in Google Chrome or Mozilla Firefox to access content", otherwise display page. Not sure if this is possible with an if else statement, or even php for that matter. Does anyone know what I'm talking about, and if so, any suggestions? in the following code I am unable to understand the purpose of @ Code: [Select] function get_categories(){ //query database for a list of categories $conn = db_connect(); $query ="select catid, catname from categories"; $result = @$conn->query($query); if (!$result){ return false; } $num_cats = @$result->num_rows; if($num_cats==0) { return false; } $result = db_result_to_array($result); return false; } function db_result_to_array($result) { $res_array = array(); for ($count=0; $row = $result->fetch_assoc(); $count++){ $res_array[$count] = $row; } return $res_array; } the more source code i see this in, =. , the more intrigued i am by it, i cant find tuts on it anywhere it cant be searched. please explain what it is and how to use it please. function quote_smart($value, $handle) { if (get_magic_quotes_gpc()) { $value = stripslashes($value); } if (!is_numeric($value)) { $value = "'" . mysql_real_escape_string($value, $handle) . "'"; } return $value; } how this script works and this script not linked to any variable or any other place so how this scripts works without linked or connected to anywhere (sorry if this ques is dumb but i m newbie in php I am working with wordpress. I am coding some custom meta boxes following some code someone else had written. My code is working but I am not sure if I need to do it the way I am doing it. The person who wrote the code added the option like so Code: [Select] $custom = get_post_meta($post->ID); $item_link = $custom["item_link"][0]; According to the wordpress codex, get_post_meta($post->ID) Returns a multidimensional array with all custom fields of a particular post or page. so when I added a second option, I followed with this Code: [Select] $item_text = $custom["item_text"][0]; I guess my question would be..Do i need to add the index 0 to every option or should I increase it by 1 for every new option I add? Can anyone explain to me why this isn't working , and explain how i would rectify the problem, Many Thanks. Code: [Select] <? $rawr = mysql_real_escape_string($_GET['type']); "SELECT COUNT(id) FROM items Where type = $rawr" ?> Hey guys, this is what I'm trying to do. I want to print on the member's profile page, a list of the products he has bought so far. I've come with two solutions: A) add a new field to the products' table, with the ID of every clients that bought that product; B) add a new field to the members table, with the ID of every products he bought; These are pretty similar approaches, but I've no idea on how can this be done. It would look like: Bought: 2, 3, 4, 5, I dont know how to write this data or how to read it. Any idea? Thanks! Sorry this may sound insane. Is there anyway to accomplish this, using paypal you can creat buy it now buttons and whatnot. The problem with these is you set the price of the time when making the button. Is there a way to set the price with php thus creating a dynamic price instead of a static one? I know you can send paypal vars. but is there a way to edit the price? Thanks I hope that makes sense at all [Edit: Sorry I found my answer by searching for "period", then finding out that it is a string operator, then read about that. Doesn't seem easy to search for ".=" in Google. Thanks] I got some help here earlier, and I can pretty much understand everything, except I don't understand why there is Code: [Select] $recordOptions .= "<option value=\"{$row['id']}\">"; $recordOptions .= " {$row['firstname']} {$row['lastname']}, {$row['gender']}, {$row['dob']}"; $recordOptions .= "</option>\n"; Why does this whole thing break without .= ? I am new to PHP so, sorry if the answer is really obvious. Code: [Select] $recordOptions = ''; $query = "SELECT * FROM people"; $result = mysql_query($query); if(!$result) { $recordOptions = "<option>Error Retrieving Records</option>\n";; } else { while($row=mysql_fetch_assoc($result)) { $recordOptions .= "<option value=\"{$row['id']}\">"; $recordOptions .= " {$row['firstname']} {$row['lastname']}, {$row['gender']}, {$row['dob']}"; $recordOptions .= "</option>\n"; } } ?> <html> <body> <?php echo $confirmMsg; ?><br /> <form action ='' method='post'> This should be an easy one but I'm looking for a bit of help. I'm using a php framework and using includes to bring different pieces of a website togethor onto the page. The top of the page uses a flash banner, then there are some simple text sections below. The banner is in it's own php file as well. Wondering if it's possible to have the banner NOT replay after each page load? I'm not sure if this is something I need to setup in the flash file, or if there is a way to do it in the php files? Basically, the banner is the same for all of the pages, and I want it (once loaded) to stay on the page while the details on the bottom change, WITHOUT going through the entire animation again. Make sense? here's the page as it stands now so you can get a better idea what I'm trying to do. http://www.getmoreimpact.com/index.php again not sure if this needs to be done in the flash side, or the php? Any ideas are better than what I've got! Thanks in advance This topic has been moved to HTML Help. http://www.phpfreaks.com/forums/index.php?topic=331697.0 Hi, I have the following code $w=$_GET['w']; $h=isset($_GET['h'])?$_GET['h']:$w; $x=isset($_GET['x'])?$_GET['x']:0; $y=isset($_GET['y'])?$_GET['y']:0; $filename=$_GET['src']; header('Content-type: image/jpg'); header('Content-Disposition: attachment; filename='.$src); $image = imagecreatefromjpeg($filename); $crop = imagecreatetruecolor($w,$h); imagecopy ( $crop, $image, 0, 0, $x, $y, $w, $h ); imagejpeg($crop); And display it as such <img src="crop.php?x=<?php echo $_GET['x1']; ?>&y=<?php echo $_GET['y1']; ?>&w=300&h=300&src=cropper/castle.jpg"> So say the image is 500 x 333, it crops out a 300x300 section of it from X and Y. Works fine. BUT, when i resize the image to say 450 x 300, it obviously still crops it from the 500 x 333 version so it looks different to what it should do. I need a way to be able to resize the image in the above PHP and then imagecopy from that. Cant think of how that would be done, any ideas? Thank you! |