PHP - New To Coding In General, Array Question
Sorry for the non code lingo, but I am wondering if I can pull a word from an array and use it to get a value from another array.
I am making a simple dodgeball game engine. I want the coaches strategy to pick a certain attribute to base who to throw to. Here is the team array:
//silverbacks
Similar TutorialsMy current function for checking over user inputs is below, email and phone numbers work just fine. I'm more worried about the insufficiency of text and textarea, this isn't being used live anywhere atm while i remake it and i'm out of ideas on how to scan general data. Fields passing through text, textarea might be full names, addresses, subjects basically general things. I've been stumbling on ideas of what would be best practice to do this. Any ideas? # $tbc = data to be cleaned # $type = email, phone, text, textarea function escape_data($tbc, $type='text') { switch($type) { case 'email': if(preg_match('/(\w+\.)*\w+@(\w+\.)*\w+(\w+\-\w+)*\.\w+/', $tbc)){ $op = $tbc; } else { $op = false; } break; case 'phone': if (!empty($tbc)) { preg_match_all('/[0-9\(\)+.\- ]/s', $tbc, $cleaned); foreach($cleaned[0] as $k=>$v) { $ready .= $v; } if ((strlen($ready) > 10) && (strlen($ready) <=25)) { $op = $ready; } else { $op = false; } } else { $op = false; } break; case 'text': case 'textarea': if (!empty($tbc)) { $op = strip_tags($tbc); } else { $op = false; } break; default: $op = false; } return $op; } I'm trying to learn how to code the proper way. There's a few things im worried about,but overall im worried about clogging up the server. so what should i keep in mind while coding, to avoid this? I'm thinking, keep insert and update to a minimum? as in, when possible, use session to keep track of data until i absolutly have to add that data to a database? is that a correct thought? what about once the data is in the database, how much trouble is it to ask for the selected data inside a database? should i be worried about overdoing request of data from databases? on the other hand, should i be worried about having too many sessions travelling around with a user. why? Im just asking to get a better feel for how to do things the proper way, i would hate to have a website built, only to find out that what i built is only a bat, that would bash the hell out of a server i wanted to put it on. This topic has been moved to Ajax Help. http://www.phpfreaks.com/forums/index.php?topic=321915.0 Hi, I am really plugging into how to write functions in PHP. But I was going to delve into a user management program and try to create it, but dont want to be the older version of what I was before if you like, where I just type in for the sake of typing in code so I thought I would question what their doing. But a peice of code, a very small snippet, this came up: @mysql_connect What does this actually mean with the @ sign infront of the mysql_connect function? Seen this a few times but just never appreciated what the at sign means, any help is wonderfully appreciated of course. Thanks, Jeremy I have a general questions. 1. I have a list of products. 2. I have a list of each of the 50 states. 3. I have a list of "professions". I need to setup something called "State Verbiage". However, this is going to be on a per product, per state, per profession level. So each profession is mapped up to each state and product. So product #1, in State #4, for profession #2 would have verbiage. Any combination of the three would have different verbiage. Any advice on the easiest way to set this up, would be appreciated. Hi there just wanted to ask you a general question regarding referenced variables and functions in PHP. I have this code: function theRefFunction(&$var){ $var = $var +1; return $var; } $a = 50; echo $thisvalue = theRefFunction($a); Just for learning purposes, as allot of times beginning PHP seriously developers who want to improve on memory consumption in PHP find this tricky, is there any point in using an example like this? I.e. would I really need the &$var as the parameter for the function called 'theRefFunction()'? Just wondered thats all, if not what would be a better way for really using it? It's just so I can go onto maybe doing a loop in it and setting it as a real example like working out tax and stuff like that, just for learning purposes, won't yet be using it. Just wanted to build up as I said earlier a library of things I have done and make maybe my own tutorial site. I look forward to any replies, Jeremy. I'm ready for the next step on my site, which is learning how to filter results down with a second search. The scenario is: A viewer searches for a plant by 4 variables to describe the leaf. After submit, they go to the results page, with 200 paginated results. (This is where I've gotten to) Now, on that same page, they choose to further narrow their results by searching those 200 results for plants that have purple flowers. Maybe they get 40 results and can use another variable to narrow further - and so on. What's the best way to set this up? I've been looking into temporary tables, but it seems to be a challenge because the table deletes the moment they leave that page or close the connection? Sessions seem like another possibility, but I read there's security issues. What's the best way to set this up? Just re-query the database with the entire set of variables from both the first and second searches? is it quicker to build some type of permanent table with a timestamp that I can use to delete it after the user has gone away? Bear with me, as I'm still quite new at PHP. I'm trying to figure out the best way to build a search function for a database that will eventually be quite large. The path of the search I'm building is to go from a straight-forward javascript and html search page, to a paginated search result. Then the user can narrow down those query results based on a new search of their results. In trying to get the pagination to work across several pages(unsuccessfuly, I might add), I've been learning about sessions and temporary tables. But, because the database will be very large, I'm wondering if they are the wrong way. Sessions have a time limit, and temporary tables delete once the connection is closed. Is it possible/feasible to build a permanent table that puts in the search variables, with a unique id and use this to manage the searches, pagination and all that? Then, at some point in the process, I can put code to delete the corresponding row (or even make it a saveable search). Maybe somebody sees where I'm going with this and can describe it better than me? I'm just thinking off the cuff at the moment. Maybe there's some terminology that will help me find a tutorial. Any bit helps. thanks! Hi All: I have general question on php... if I have an "includes" folder, and on IE somebody goes directly to it, like: http://www.site.com/includes on IE, they can see the folder-tree- directory. So, files like config.php, or mysql.php could be visible. My question is, is this safe?? couldnt people then just drag-copy the file over into their system? or somehow open it, view its contents? I dragged-copied a .php file into my desktop, but nothing shows up when I open in in notepad. As opposed to opening another file that was also .php but which only had javascript code in it, and the javascipt did show up. So, I take it, it should be ok?? as it seems like it works as if the .php page actually had been loaded on a web-browser... doesnt show the php code... well, appreciate comments, feedback on this... what woudl be the best way to protect files like this? htaccess-protect? or is this not something to be overly concerned about?? From what I can tell looks like a lot of site are setup this way... so must be ok... ha? Thanks! Hey guys, Why exactly do people use multiplication when calculating time? Like when setting cookies, timeouts or something that requires second-based definitions. For example, some do: Code: [Select] $thirty_days = time()+60*60*24*30; As opposed to what I do: Code: [Select] $thirty_days = time()+2592000; Why do they do that? Does it help them calculate it without a calculator? Make it easier to manage/adjust in the future? Personal preference? These are the only possible explanations I can think of. This has been bugging me for a while...am I doing something wrong? I can't get the COUNT of the matching results? Please help. $SomeVar = myname; $queryU = "SELECT * FROM adxone WHERE username = '".$SomeVar."'"; $resultU = mysql_query($queryU); while($scoreP = mysql_fetch_array($resultU)) { $scoreP['roundzAa']; $scoreP['roundzAb']; $scoreP['roundzAc']; $scoreP['roundzAd']; $scoreP['roundzAe']; $scoreP['roundzAf']; $scoreP['roundzAg']; $scoreP['roundzAh']; } $WinV = 'resultA'; $query = "SELECT * FROM acs WHERE resultx = '".$WinV."'"; $result = mysql_query($query); while($scoreUsr = mysql_fetch_array($result)) { $scoreUsr = $scoreM1['winxa']; $scoreUsr = $scoreM1['winxb']; $scoreUsr = $scoreM1['winxc']; $scoreUsr = $scoreM1['winxd']; $scoreUsr = $scoreM1['winxe']; $scoreUsr = $scoreM1['winxf']; $scoreUsr = $scoreM1['winxg']; $scoreUsr = $scoreM1['winxh']; } $count1 = count( array_intersect($resultU, $result) ); echo($count1); mysql_query("UPDATE comp SET id=$count1 WHERE username = '".$SomeVar."'"); ?> I am trying to convert Numbers to months from mySQL. What I current have in my db is smonth = # What I am trying to figure out is if there is a way to do an array that will convert the # to a month instead of use if $smonth = 1 echo January. Any help is greatly appreciated and thank you in advance. I have a question about the array in this code... Quote class Model { public function getBookList() { return array( "Jungle Book" => new Book("Jungle Book", "R. Kipling", "A classic book."), "Moonwalker" => new Book("Moonwalker", "J. Walker", ""), "PHP for Dummies" => new Book("PHP for Dummies", "Some Smart Guy", "") ); } } Quote class Book { public $title; public $author; public $description; public function __construct($title, $author, $description) { $this->title = $title; $this->author = $author; $this->description = $description; } } Questions: -------------- 1.) Is it correct that in the array above, the KEY is a book name and the VALUE is an object? 2.) What is the name of the array above? TomTees Hi I'm looking something simular to this: http://cupaddon.com/index.php?site=halloffame Under the place column, it shows #1 #2 #3 for each row. How can I use php to use this on my table for each row? And please say if this question makes no sense to you. I simply want to use #1 #2 etc for each of my rows for my table. I am using a while loop for my table also. I really appreciate if anyone can help me on this, thanks very much! I have gotten a lot of help with my php to get, read and parse and xml file for NFL scores... it was working really great but then I found something and now I have a new problem...
Currently it reads as follows:
$url="http://www.nfl.com/liveupdate/scorestrip/postseason/ss.xml "; $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_URL, $url); // get the url contents $data = curl_exec($ch); // execute curl request curl_close($ch); $xml = simplexml_load_string($data); $games = array( 'REG' => array(), 'WC' => array(), 'DIV' => array(), 'CON' => array(), 'SB' => array() ); foreach($xml->gms->g as $game) { // it's a single game // if (!empty($game['vtn'])) { $games[(string) $game['gt']][] = $game; } } ?> <div class='ticker'> <ul> <?php foreach ($games as $gametype => $subgames) { foreach ($subgames as $game) { if ($game['gt'] == 'REG') { ?> <li data-subcategory='Regular Season' data-category='Week <?= $game['w'] ?>' data-color='1979ab'> <a href='#'><b><?= $game['h'] ?> <?= $game['hs'] ?> <?= $game['v'] ?> <?= $game['vs'] ?></b></a> </li> <?php } else if ($game['gt'] == 'WC') { ?> <li data-subcategory='Round 1' data-category='Wild Card Games' data-color='1979ab'> <a href='#'><b><?= $game['htn'] ?> <?= $game['hs'] ?> <?= $game['vtn'] ?> <?= $game['vs'] ?></b></a> </li> <?php } else if ($game['gt'] == 'DIV') { ?> <li data-subcategory='Round 2' data-category='Divisional Games' data-color='14ab05'> <a href='#'><b><?= $game['htn'] ?> <?= $game['hs'] ?> <?= $game['vtn'] ?> <?= $game['vs'] ?> <?= $game['d'] ?> at <?= $game['t'] ?></b></a> </li> <?php } else if ($game['gt'] == 'CON') { ?> <li data-subcategory='Championship Games' data-category='Conference Games' data-color='000000'> <a href='#'><b><?= $game['htn'] ?> <?= $game['hs'] ?> <?= $game['vtn'] ?> <?= $game['vs'] ?> <?= $game['d'] ?> at <?= $game['t'] ?></b></a> </li> <?php } else if ($game['gt'] == 'SB') { ?> <li data-subcategory='<img src=localhost/joomla/modules/mod_scores/images/nfl.png>' data-category='Super Bowl' data-color='000000'> <a href='#'><b><?= $game['htn'] ?> <?= $game['hs'] ?> <?= $game['vtn'] ?> <?= $game['vs'] ?> <?= $game['d'] ?> at <?= $game['t'] ?></b></a> </li> <?php } } } ?> </ul> </div>I did notice that this will work fantastic during the post season but in regular season it's a new issue... the post season xml reads out team names likes this "Carolina Panthers" as one of the elements.. however during the regular season that gets shortened to: "CAR". How do I get it to read the xml and change "CAR" Into "Caroline Panthers"? I have to do this for all 32 teams... Thanks everyone! I have several fields in a database in which some could be blank. I'd like to echo out 'Not Assigned' if that's the case. Based on my limited experience with PHP this is the way I'm going about it: Code: [Select] if ($tech == "") { $tech = "Not Assigned"; } if ($crew == "") { $crew = "Not Assigned"; } if ($number == "") { $number = "Not Assigned"; } I'm pretty certain there is a way to simply place all of the variables into an array to determine if they are empty or not - Followed by changing the variable to 'Not Assigned' one time rather then repeating code over and over again. The way I'm doing it will work, however, something tells me there is a much more efficient way. Hi, I have an array that I've set up as a JSON feed API. The array pulls data from a MySQL query, one of the fields in the query ('description') contains text with escape characters. I'm just wondering how I change either the query to remove the escape characters (ie can you strip escape chars in MYSQL?) - or change the array to strip the escape chars?? Code: [Select] //Run query $result = mysql_query(' SELECT DATE_FORMAT(gl_date,"%d-%m-%Y")as "date", gl_venue as "venue", gl_city as "city", gl_postcode as "postcode", gl_text as "description", concat(DAYOFMONTH(gl_date), MONTHNAME(gl_date),".png") AS "imageName" FROM tg_gig_list where gl_date >= curdate() and gl_publish = 1 order by gl_date '); $array = array(); $array['gigs'] = array(); while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $array['gigs'][] = $row; } $output = json_encode($array); } Hey guys,
I am using a function that echos out 10 images from my database. I need to have a minimum of 4 echoed images. Some categories have less than 4 images, so I would like to have a default image(s) echoed out if the database has 4 or less images.
Does anyone know how I can do this?
Below is the code I am using now to echo out the 10 images.
Thanks in advance:
<?php $query = mysql_query('SELECT image_id FROM images ORDER BY RAND() LIMIT 10'); $i = 0; class myCounter3 implements Countable { public function count() { static $count = 0; return ++$count; } } $counter = new myCounter3; while ($row = mysql_fetch_array($query)) { if($i % 10 === 0) { } echo '<img src="http://www.mysite.com/'.$row['image_id'].'.jpg" width="300" height="auto"/>'; $i++; } ?> Default Image: <img src="http://www.mysite.com/default.jpg" width="300" height="auto"/> Hey, Can someone please fix my code below? I simply want to add all values for $row[score1] in first if statement and add all values for $row[score2] in second if statement and then add them together to return final value . Code: [Select] while( $row = mysql_fetch_assoc($query)) { if($clanID==$row[clan1] && $row[score1] > $row[score2]) { $points[] = $row[score1]; } if($clanID==$row[clan2] && $row[score1] < $row[score2]) { $points[] = $row[score2]; } } return $points;. So lets say I echo the variables that I want added... Code: [Select] if($clanID==$row[clan1] && $row[score1] > $row[score2]) { $points[] = $row[score1]; echo $row['score1']."<br>"; } if($clanID==$row[clan2] && $row[score1] < $row[score2]) { $points[] = $row[score2]; echo $row['score2']."<br>"; } On page it returns as: 11 16 14 Add it together is 41 and this is what I want the output to be. Hope you understand I appreciate any help! P.S. I must be able to return final value OUTSIDE loop. Thanks. Basically i want to take an array of numbers let's say: Code: [Select] <? $array1=array("1","2","3","4"); ?> Now say i reprint that same array, but the 3 is missing. Code: [Select] <? $array2=array("1","2","4"); ?> How would i print what's missing from $array1 out of $array2? Sorry if this is confusing, i'll be happy to go into details if needed. |