PHP - Array Question
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. Similar TutorialsHi 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 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 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"/> 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. 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. I am fairly new to PHP, coming over from JAVA. In JAVA, you can select an element of an array and set it as a separate variable, such as: String name="": name = array[4]; But in PHP I am trying this: $ban = $banners[$selected_banner]; Where $selected_banner is a random number. Unfortunately, I am gettting the following error... Catchable fatal error: Object of class stdClass could not be converted to string Are arrays in PHP able to be used in such a way? What am I missing? Hey guys I have an array and want to make it into a sting and separate each array segment by a comma...is there a function for this or should I just do it in a loop myself?...thanks I will state right here that I've never really delved into PHP before, but I'm prying apart the code on my site (a programmer made it for me). I've figured out how to add more checkboxes onto the page, but I've run into a problem. Code: [Select] 'Artist' => array ( 'Cassiadawn', 'Cryptic', 'Diction', 'Glorfindel', 'Kyrislian', 'Russa', 'Ntkufreak', 'Taliba', 'Thunderbun', 'Velg', 'Lostdollie', 'Pookawitch') What I'm trying to do is replace 'Lostdollie', 'Pookawitch' with 'Lostdollie [R]', 'Pookawitch[R]'. How can I do this? I'd really appreciate any help anyone can give me! Hello. I have a newbie question regarding arrays: How can I echo an element's subsequent arrays and elements? For example Code: [Select] $pers = array("Steve" => array("eyes" => "green", "age" => "43", "race" => "caucasian")); and if I echo $pers["Steve"] to make it display: green 43 caucasian. Thanks. Hi. I have two arrays: $one = array('a', 'b', 'c'); $two = array('123', '456', '789'); My desired output is: $output = array('123a', '123b', '123c', '456a', '456b', '456c', '789a', '789b', '789c' ) How can this be achieved, I have looked into functions such as array_merge() but it doesnt seem to do the job. Help greatly appreciated! Ok I have a multidimensional array and what I want to know is how to use it. I need to pick the items from the array but only the items specified or individual keys in the array how would I go about doing that. Here is the array: <?php $monstors = array ( "Skeleton"=>array ( "mob_name"=>"Skeleton", "mob_attack"=>"5", "mob_hp"=>"30", "mob_level"=>"1" ), "Skeleton Fighter"=>array ( "mob_name"=>"Skeleton Fighter", "mob_attack"=>"10", "mob_hp"=>"35", "mob_level"=>"2" ), "Skeleton Warrior"=>array ( "mob_name"=>"Skeleton Warrior", "mob_attack"=>"15", "mob_hp"=>"40", "mob_level"=>"3" ) ); ?> How would I like print_r the element in the array called Skeleton Warrior? 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. Quick question, When I run the code below I get an undefined index error for the array. If I define all the indexes I have no problem making it work correctly. I thought writing it like I did below would define the indexes as they are needed. What am I doing wrong? Thanks for looking, $call_data = array(); if($call_segment['did_digits'] == '3300'){ //Check if call was answered if($call_segment['answered'] == 'No'){ $call_data['3300']['abandoned'] ++; } else{ if(in_array($call_segment['finished_on'],$phoneroom_ext)){ $call_data['3300']['phoneroom'] ++; } elseif(in_array($call_segment['finished_on'],$anoka_ext)){ $call_data['3300']['store'] ++; } } } 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! 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); } my php array knowledge is still basic and i can't seem to get this code to yield an answer in the form i want. i would like to be able to get at $m and $b separately so I can manipulate them. how, for example do i just print $m? Code: [Select] <?php var_dump( linear_regression(array(1, 2, 3, 4), array(1.5, 1.6, 2.1, 3.0)) ); /** * linear regression function * @param $x array x-coords * @param $y array y-coords * @returns array() m=>slope, b=>intercept */ function linear_regression($x, $y) { // calculate number points $n = count($x); // ensure both arrays of points are the same size if ($n != count($y)) { trigger_error("linear_regression(): Number of elements in coordinate arrays do not match.", E_USER_ERROR); } // calculate sums $x_sum = array_sum($x); $y_sum = array_sum($y); $xx_sum = 0; $xy_sum = 0; for($i = 0; $i < $n; $i++) { $xy_sum+=($x[$i]*$y[$i]); $xx_sum+=($x[$i]*$x[$i]); } // calculate slope $m = (($n * $xy_sum) - ($x_sum * $y_sum)) / (($n * $xx_sum) - ($x_sum * $x_sum)); // calculate intercept $b = ($y_sum - ($m * $x_sum)) / $n; return array("m"=>$m, "b"=>$b); } ?> if(in_array("b", $array)) { } The above checks to see if a value is present in an array, but how do I then retrieve the array key value from the matching value? $array[0] = "a"; $array[1] = "b"; $array[2] = "c"; So in the example, the answer is 1 (array key 1). Hi there, I have the following PHP code: <?php $books = array( array("title"=>"Book Title 1", "author"=>"Author 1", "publisher"=>"Publisher 1"), array("title"=>"Book Title 2", "author"=>"Author 2", "publisher"=>"Publisher 2"), array("title"=>"Book Title 3", "author"=>"Author 3", "publisher"=>"Publisher 3"), ); foreach ($books as $key => $value) { echo "$key : $value<br>"; } ?> I'm trying to loop through this code, but all it outputs is: 0: ARRAY 1: ARRAY 2: ARRAY -- It's not even pulling the proper $keys and $values. It works when there's just ONE array -- but not with this multidimensional array. Any ideas? Thanks! J I am having trouble understanding what this means: foreach($_SESSION['cart'] as $product_id => $quantity) I look forward to any responses in advance. 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 |