PHP - Generate Numbers From Array Except For The Ones From Another Array
$bl = array();
$link1 = array_rand($cds); $link2 = array_rand($cds); $link3 = array_rand($cds); $link4 = array_rand($cds); $A = 0; while (!in_array($link1, $bl)){ $link1 = array_rand($cds); $bl[$A] = $link1; $A++; } while (!in_array($link2, $bl)){ $link2 = array_rand($cds); $bl[$A] = $link2; $A++; } while (!in_array($link3, $bl)){ $link3 = array_rand($cds); $bl[$A] = $link3; $A++; } while (!in_array($link4, $cds)){ $link4 = array_rand($cds); $bl[$A] = $link4; $A++; } echo $link1; echo $link2; echo $link3; echo $link4; Pretty much I'm trying to generate 4 number's between the min and max of an array but I don't want any duplicates. So it checks if the number exists in a bl array if it does it generates another number, well thats the theory. Doesn't work like that though. Similar TutorialsNOTE - Please read the information first as it contains important information to understand the problem. Rules → • There are 9 Columns(C1,C2,C3,C4,C5,C6,C7,C8,C9) [ Max columns will be 9] • The number of Rows can vary from 3,6,9,12,15,18 (Max). In this case Number of Rows shall be 12 Number of Rows = No of Tickets (Max Allowed 6) x Rows Per Ticket (Max Allowed 3). Thus, Max Rows can be 18 • Each Row is required to have 4 Blank Spaces and 5 Filled with Numbers • All numbers available in the Column Array have to be utilized • This configuration of an shall create a matrix of 9 Columns & 12 Rows (3 x 4 Tickets), which is 108 MATRIX BLOCKS where only a maximum of 60 numbers can be filled out of 108 available blocksrandomly with the above conditions being met 100%. • The numbers in column must be arranged / sorted in ASCENDING ORDER (For coding logic purpose, as soon as the number is assigned to the new MATRIX MAP use array_shift() or unset() the number so as to avoid repetition Example - Row 1 and Column 1 shall generate a MATRIX BLOCK - R1C1 Row 3 and Column 7 shall generate a MATRIX BLOCK - R3C7 Matrix Block can also be termed as Matrix Cell for your ease (if needed) MASTER SET OF ARRAY WITH NUMBERS array( "C1"=> array( 1, 2, 3, 5, 6, 7, 9 ), //7 Numbers "C2"=> array( 13, 14, 15, 17, 18, 19 ), //6 Numbers "C3"=> array( 21, 22, 23, 24, 25, 26, 30 ), //7 Numbers "C4"=> array( 31, 33, 34, 36, 37, 38, 39 ), //7 Numbers "C5"=> array( 41, 42, 46, 47, 48, 49, 50 ), //7 Numbers "C6"=> array( 51, 52, 53, 54, 55, 57, 58 ), //7 Numbers "C7"=> array( 61, 62, 64, 65, 69, 70 ), //6 Numbers "C8"=> array( 71, 74, 75, 76, 77, 78 ), //6 Numbers "C9"=> array( 82, 83, 85, 87, 88, 89, 90 ) //7 Numbers ); The above array has 60 Numbers to be filled out of 108 MATRIX BLOCK / CELL which meets the condition that for a FULL BLOCK containing 4 MINI BLOCKS WITH 3 ROWS (max. allowed) EACH I have been able to generate this without any issue meeting all the conditions of the Columns My Allocation Matrix Array will look like array( "R1"=> array( "C1"=> true, // Means that MATRIX BLOCK R1C1 will be NOT EMPTY "C2"=> false, // Means that MATRIX BLOCK R1C2 will be EMPTY "C3"=> true, "C4"=> false, "C5"=> true, "C6"=> false, "C7"=> true, "C8"=> true, "C9"=> false ), "R2"=> array( "C1"=> false, "C2"=> true, "C3"=> false, "C4"=> true, "C5"=> false, "C6"=> true, "C7"=> true, "C8"=> true, "C9"=> false ), "R3"=> array( "C1"=> true, "C2"=> true, "C3"=> true, "C4"=> true, "C5"=> false, "C6"=> false, "C7"=> false, "C8"=> false, "C9"=> true ), "R4"=> array( "C1"=> true, "C2"=> true, "C3"=> true, "C4"=> false, "C5"=> true, "C6"=> true, "C7"=> false, "C8"=> false, "C9"=> false ), "R5"=> array( "C1"=> false, "C2"=> false, "C3"=> false, "C4"=> false, "C5"=> true, "C6"=> true, "C7"=> true, "C8"=> true, "C9"=> true ), "R6"=> array( "C1"=> true, "C2"=> true, "C3"=> false, "C4"=> true, "C5"=> false, "C6"=> true, "C7"=> false, "C8"=> false, "C9"=> true ), "R7"=> array( "C1"=> false, "C2"=> false, "C3"=> true, "C4"=> false, "C5"=> true, "C6"=> false, "C7"=> true, "C8"=> true, "C9"=> true ), "R8"=> array( "C1"=> true, "C2"=> false, "C3"=> false, "C4"=> true, "C5"=> false, "C6"=> false, "C7"=> true, "C8"=> true, "C9"=> true ), "R9"=> array( "C1"=> true, "C2"=> false, "C3"=> true, "C4"=> false, "C5"=> true, "C6"=> true, "C7"=> false, "C8"=> false, "C9"=> true ), "R10"=> array( "C1"=> false, "C2"=> true, "C3"=> true, "C4"=> true, "C5"=> true, "C6"=> false, "C7"=> true, "C8"=> false, "C9"=> false ), "R11"=> array( "C1"=> false, "C2"=> true, "C3"=> false, "C4"=> true, "C5"=> true, "C6"=> true, "C7"=> false, "C8"=> true, "C9"=> false ), "R12"=> array( "C1"=> true, "C2"=> false, "C3"=> true, "C4"=> true, "C5"=> false, "C6"=> true, "C7"=> false, "C8"=> false, "C9"=> true ) ); In the above array R stands for Row, C for Column, TRUE/FALSE (Boolean) means that if TRUE a Number can be filled in the resulting MATRIX BLOCK / CELL ( Row[Number]Column[Number] ) else if FALSE the MATRIX BLOCK / CELL shall be EMPTY The result for the above shall be
PROBLEM : I am unable to understand what should possibly be the logic & loop used here for creating a MATRIX ALLOCATION MAP as shown above I have tried while, foreach & for but unable determine the perfect combination which would meet the conditions. (Tried all of the above with Nested Loops also) Edited May 1, 2020 by AlphaMikeTags public function getEventTypes() { return array( 1=>array('Exhibition', 1), 2=>array('Wedding', 1), 3=>array('Business', 1), 4=>array('School Trip', 0), 5=>array('Birthday', 0), 6=>array('Concert', 1), 7=>array('Sporting Event', 1), 8=>array('Training', 0), 9=>array('Conference', 1), 10=>array('Other', 1), ); } I now want to generate a new array from this array as follows: 1) must retain the existing key 2) must be single dimensional and the value of each element will be value[0] 3) must only contain the array elements where value[1] = 1 So for example the generated array should be as follows: public function getActiveEventTypes() { return array( 1=>'Exhibition', 2=>'Wedding', 3=>'Business', 6=>'Concert', 7=>'Sporting Event', 9=>'Conference', 10=>'Other', ); } Here is how I get categories for article listing. Article can be written in several categories at once. So I have this code: Code: [Select] $category = '1,5,23,46'; //Category IDs $get_cats = explode("," , $category); $cats = ""; for ($i = 0; $i<count($get_cats); $i++ ) { $cat = trim($get_cats[$i]); $catSlug = get_value_of($cat, "inc/cat_ids.php"); $catName = str_replace("-"," ",$catSlug); $cats .= "<a href=\"index.php?category=$catSlug\">".strtoupper($catName)."</a>, "; } $cats = substr($cats,0,-2); As you can see there's get_value_of function that is used to read the category from the file. File is cat_ids.php and looks like this: Quote 1='category-name-1'; 2='category-name-2'; 3='category name-3'; 4='category-name-4'; 5='category-name-5'; etc. I want to improve this in order not to read the file at each step ("for" loop). Means, one can load the file and then generate a list of (links) category compared to the numbers (Ids). So if $category is "1,3,4" I want to get list like this (by reading cat_ids.php): category-name-1, category name-3, category-name-4 With current function I read value for every ID from file in every step. So if article have 10 or more categories script load file 10 or more times. Looking for a solution to do this from a reading file only once. Thanks. I have this array of items in my php file.
$items = array( array("id" => 100,"categorypath" => "level1/powders", "product" => "Mens powder"), array("id" => 200,"categorypath" => "level1/oils/sunflower", "product" => "XYZ oil"), array("id" => 300,"categorypath" => "level1/eatable/vegetables", "product" => "carrot"), array("id" => 400,"categorypath" => "level1/oils/sunflower", "product" => "ABC oil"), array("id" => 500,"categorypath" => "level1/eatable/fruits", "product" => "mango"), array("id" => 600,"categorypath" => "level1/eatable/vegetables", "product" => "spinach"), array("id" => 700,"categorypath" => "level2/baby items/toys", "product" => "puzzle block"), array("id" => 800,"categorypath" => "level2/baby items/toys", "product" => "trucks and cars"), array("id" => 900,"categorypath" => "level2/baby items/clothes", "product" => "shirts"), array("id" => 1000,"categorypath" => "level1/powders", "product" => "Womens powder"), array("id" => 1100,"categorypath" => "level1/oils/groundnut", "product" => "GN oil"), ); Using the above array I am trying to generate a JSON file that will have the following structu
{ "category":[ { "categoryName":"level1", "category":[ { "categoryName":"powders", "products":[ { "id":"100", "path":"level1/powders", "prodname":"Mens powder" }, { "id":"1000", "path":"level1/powders", "prodname":"Womens powder" } ] }, { "categoryName":"oils", "category":[ { "categoryName":"sunflower", "products":[ { "id":"200", "path":"level1/oils/sunflower", "prodname":"XYZ oil" }, { "id":"400", "path":"level1/oils/sunflower", "prodname":"ABC oil" } ] }, { "categoryName":"groundnut", "products":[ { "id":"1100", "path":"level1/oils/groundnut", "prodname":"GN oil" } ] } ] }, { "categoryName":"eatable", "category":[ { "categoryName":"vegetables", "products":[ { "id":"300", "path":"level1/eatable/vegetables", "prodname":"carrot" }, { "id":"600", "path":"level1/eatable/vegetables", "prodname":"spinach" } ] }, { "categoryName":"fruits", "products":[ { "id":"500", "path":"level1/eatable/fruits", "prodname":"mango" } ] } ] } }, { "categoryName":"level2", "category":[ { "categoryName":"baby items", "category":[ { "categoryName":"toys", "products":[ { "id":"700", "path":"level2/baby items/toys", "prodname":"puzzle blocks" }, { "id":"800", "path":"level2/baby items/toys", "prodname":"trucks and cars" } ] }, { "categoryName":"clothes", "products":[ { "id":"900", "path":"level2/baby items/clothes", "prodname":"shirts" } ] } ] } ] } Not being an expert in php, I have somehow managed to reach thus far in my code, but can not quite arrive at the correct logic. Having trouble with handling associative arrays and objects in php. (Javascripting is much easier I feel) So far I have managed to fix all the errors/warning in my code. Here is my full php code:
$items = array( array("id" => 100,"categorypath" => "level1/powders", "product" => "Mens powder"), array("id" => 200,"categorypath" => "level1/oils/sunflower", "product" => "XYZ oil"), array("id" => 300,"categorypath" => "level1/eatable/vegetables", "product" => "carrot"), array("id" => 400,"categorypath" => "level1/oils/sunflower", "product" => "ABC oil"), array("id" => 500,"categorypath" => "level1/eatable/fruits", "product" => "mango"), array("id" => 600,"categorypath" => "level1/eatable/vegetables", "product" => "spinach"), array("id" => 700,"categorypath" => "level2/baby items/toys", "product" => "puzzle block"), array("id" => 800,"categorypath" => "level2/baby items/toys", "product" => "trucks and cars"), array("id" => 900,"categorypath" => "level2/baby items/clothes", "product" => "shirts"), array("id" => 1000,"categorypath" => "level1/powders", "product" => "Womens powder"), array("id" => 1100,"categorypath" => "level1/oils/groundnut", "product" => "GN oil"), ); $jsonStruct = array(); function jsonCreateStruct(){ GLOBAL $items; for($c=0; $c<count($items); $c++){ $categ = $items[$c]["categorypath"]; insertJson($categ, $items[$c]); } } function insertJson($catg, $itm){ GLOBAL $jsonStruct; $exp = explode("/",$catg); print_r("\n\n\n $catg \n"); if(count($exp) == 1){ print_r("Level 1 \n"); if(!isset($jsonStruct[0])){ $jsonStruct[0]["categoryName"] = $exp[0]; $jsonStruct[0]["products"] = array($itm); print_r("\nCreated:: $exp[0]"); }else{ $notFound = true; for($j=0; $j<count($jsonStruct); $j++){ $catgName = $jsonStruct[$j]["categoryName"]; print_r("\nFound: $catgName"); if($catgName == $exp[0]){ $notFound = false; if($jsonStruct[$j]["products"]){ array_push($jsonStruct[$j]["products"],array($itm)); }else{ $jsonStruct[$j]["products"] = array(); array_push($jsonStruct[$j]["products"],array($itm)); } } } if($notFound){ print_r("\nNotFound\n"); array_push($jsonStruct,array("categoryName"=> $exp[0], "products" => array($itm))); } } } if(count($exp) == 2){ print_r("Level 2 \n"); if(!isset($jsonStruct[0])){ $jsonStruct[0]["categoryName"] = $exp[0]; $jsonStruct[0]["categorypath"] = array("categoryName" => $exp[1], "products" => array($itm)); print_r("\nCreated:: $exp[0] / $exp[1]"); }else{ $notFound1 = true; $notFound2 = true; $indexLevel = null; for($j=0; $j<count($jsonStruct); $j++){ $catgName1 = $jsonStruct[$j]["categoryName"]; print_r("\nFound: $catgName1"); if($catgName1 == $exp[0]){ $notFound1 = false; $indexLevel = $j; if(isset($jsonStruct[$j]["categorypath"])){ $level1 = $jsonStruct[$j]["categorypath"]; for($m=0; $m<count($level1); $m++){ if(isset($level1[$m]["categoryName"])){ $catgName2 = $level1[$m]["categoryName"]; print_r("\nFound: $catgName2"); if($catgName2 == $exp[1]){ $notFound2 = false; if($level1[$m]["products"]){ array_push($jsonStruct[$j]["categorypath"][$m]["products"],array($itm)); }else{ $jsonStruct[$j]["categorypath"][$m]["products"] = array(); array_push($jsonStruct[$j]["categorypath"][$m]["products"],array($itm)); } } } } } } } if($notFound1){ print_r("\nNotFound1\n"); array_push($jsonStruct,array("categoryName"=> $exp[0], "categorypath" => array("categoryName" => $exp[1],"products" => array($itm)))); }else if($notFound2){ print_r("\nNotFound2\n"); // $jsonStruct[$indexLevel]["categorypath"] = array("categoryName"=> $exp[1], "products" => array($itm)); $jsonStruct[$indexLevel]["categorypath"] = array(); array_push($jsonStruct[$indexLevel]["categorypath"], array("categoryName"=> $exp[1], "products" => array($itm))); } } } if(count($exp) == 3){ print_r("Level 3 \n"); if(!isset($jsonStruct[0])){ $jsonStruct[0]["categoryName"] = $exp[0]; $jsonStruct[0]["categorypath"] = array("categoryName" => $exp[1], "categorypath" => array("categoryName" => $exp[2], "products" => array($itm))); print_r("\nCreated:: $exp[0] / $exp[1] / $exp[2]"); }else{ $notFound1 = true; $notFound2 = true; $notFound3 = true; $indexLevel1 = null; $indexLevel2 = null; for($j=0; $j<count($jsonStruct); $j++){ $catgName1 = $jsonStruct[$j]["categoryName"]; print_r("\nFound: $catgName1"); if($catgName1 == $exp[0]){ $notFound1 = false; $indexLevel1 = $j; if(isset($jsonStruct[$j]["categorypath"])){ $level2 = $jsonStruct[$j]["categorypath"]; for($m=0; $m<count($level2); $m++){ if(isset($level2[$m]["categoryName"])){ $catgName2 = $level2[$m]["categoryName"]; print_r("\nFound: $catgName2"); if($catgName2 == $exp[1]){ $notFound2 = false; $indexLevel2 = $m; if(isset($level2[$m]["categorypath"])){ $level3 = $level2[$m]["categorypath"]; for($n=0; $n<count($level3); $n++){ //print_r($level3["categoryName"]); if(isset($level3["categoryName"])){ $catgName3 = $level3["categoryName"]; print_r("\ncatgName3: ". $catgName3); if($catgName3 == $exp[2]){ $notFound3 = false; if($level3["products"]){ print_r("\npushing into array\n"); array_push($jsonStruct[$j]["categorypath"][$m]["categorypath"]["products"],array($itm)); }else{ print_r("\ncreate new and pushing into array\n"); $jsonStruct[$j]["categorypath"][$m]["categorypath"][$n]["products"] = array(); array_push($jsonStruct[$j]["categorypath"][$m]["categorypath"]["products"],array($itm)); } } } } } } } } } } } if($notFound1){ print_r("\nNotFound1\n"); array_push($jsonStruct, array("categoryName"=> $exp[0], "categorypath" => array("categoryName" => $exp[1],"products" => array($itm)))); }else if($notFound2){ print_r("\nNotFound2\n"); if(!$jsonStruct[$indexLevel1]["categorypath"]){ $jsonStruct[$indexLevel1]["categorypath"] = array(); } array_push($jsonStruct[$indexLevel1]["categorypath"], array("categoryName"=> $exp[1], "categorypath" => array("categoryName" => $exp[2], "products" => array($itm)))); }else if($notFound3){ print_r("\nNotFound3\n"); //$jsonStruct[$indexLevel1]["categorypath"][$indexLevel2]["categorypath"] = array("categoryName"=> $exp[2], "products" => array($itm)); if(!$jsonStruct[$indexLevel1]["categorypath"][$indexLevel2]["categorypath"]){ $jsonStruct[$indexLevel1]["categorypath"][$indexLevel2]["categorypath"] = array(); } array_push($jsonStruct[$indexLevel1]["categorypath"][$indexLevel2]["categorypath"], array("categoryName"=> $exp[2], "products" => array($itm))); } } } } jsonCreateStruct(); print_r("----------------------------------------"); print_r($jsonStruct); // echo json_encode($jsonStruct);
I have now reached a point where I desperately need help from experts.
I'm trying to set different colors on rows of a table using an array, Problem is I cannot get it to work right, i'm not sure where to place my for loop so that it works right and generates a different color for different row. Code: [Select] $color = array(red,green,blue); while ($row = mysql_fetch_assoc($exec)) { echo '<tr>'; echo '<td>'.$row['brand_name'].'</td>'; echo '<td>'.$row['contact_name'].'</td>'; echo '<td>'.$row['contact_info'].'</td>'; echo '<td>'.$row['email'].'</td>'; echo '<td>'.$row['description'].'</td>'; echo '</tr>'; } I tried putting for loop only for tr, but it didnt work. Code: [Select] for ($i=0;$i<=count($color);$i++) { echo "<tr bgcolor=\"".$color[$i]."\">"; } I cannot put it within the while loop, it'll repeat the rows for all the colors. So where exactly do I place the for loop so that it works right? I have a MySQL table with a list of albums and there is a field called "views" with the number of views each album has received. I'm looking to generate an array of all the albums in the table and sort the array by the number of views (descending). I have a list of functions defined in a ContentController.php file. I created a new function called build_albumlist, which I've pasted below. The function "get_ip_log" already exists and works, and I used it as a template to create the "build_albumlist" function: public function build_albumlist(){ return $this->select_raw("SELECT * FROM albums WHERE deleted = '0' ORDER BY views DESC",array(),'all'); } public function get_ip_log(){ return $this->select_raw("SELECT * FROM sessions ORDER BY ID DESC",array(),'all'); } When I use the function, I get this warning: Warning: mysql_real_escape_string() expects parameter 1 to be string, array given inC:\xampp\htdocs\Controllers\DBController.php on line 10 [font=cabin, 'trebuchet ms', helvetica, arial, sans-serif]The "select_raw" function that I used in "build_albumlist" is defined in the DBController.php file, and is defined as below:[/font] private function clean_array($params){ $final=array(); foreach($params as $param){ $final[]=mysql_real_escape_string($param); } return $final; } public function select_raw($query,$params,$type=''){ $query=str_replace("?","'%s'",$query); $final_query= call_user_func_array('sprintf', array_merge((array)$query, $this->clean_array($params))); if($type==''){ $result=mysql_query($final_query) or die(mysql_error()); return mysql_fetch_assoc($result); } elseif($type=='all'){ $result=mysql_query($final_query) or die(mysql_error()); $final=array(); while($row=mysql_fetch_assoc($result)){ $final[]=$row; } return $final; } Does anyone know why the "build_albumlist" function is generating this warning, while the "get_ip_log" is not? Any help would be great, as I am obviously pretty new to this. Been looking to see if anyone has published something like this. I have a number of projects coming up which entail creating input form pages using PHP. Some of these are quite large as to the number of entries, pages etc.. and the worst part is that I know in advance that they most likely will want to change/add to the form after I am done. So rather than coding them physically in the code, I want to define the form elements in an array that I can store in a database/flat file and when the page loads, read from the array to construct the form output. I don't have any issues with dealing with the database or even using arrays, but I have not been able to come up with an array setup that works. Has anyone done this and would care to share the structure they used. Everything that I find on the net is basically sites that want to host the form, but this will be in a non-Internet accessible site so that is not an option. I have a cards array, and I have this function which will shuffle the cards for me. How can I edit this to 'deal' 12cards (6 & 6) until all the cards are a specific 6 & 6 I pick out? Code: [Select] function ShuffleCards(&$cardsArray, $times) { // Randomizes where to split within center (-3 to +3 from dead center) (MINIMUM 10 CARDS IN DECK) // Splits into two array. Randomizes how many cards from left and how many cards from right (between 1 and 3) // Alternates sides. Continues until both arrays are empty. $arraySize = count($cardsArray); if($arraySize<10) return; $deadCenter = $arraySize/2; for($i=0;$i<$times;$i++) { reset($cardsArray); $cutVariant = rand(-3, 3); $cutLocation = $deadCenter+$cutVariant; $firstArray = array(); $secondArray = array(); for($z=0;$z<$arraySize;$z++) { if($z<$cutLocation) array_push($firstArray, $cardsArray[$z]); else array_push($secondArray, $cardsArray[$z]); } $bottomFirst = rand(0, 1); $cardsArray = array(); while(count($firstArray) && count($secondArray)) { $leftNewCards = array(); $rightNewCards = array(); $leftVariant = rand(1, 3); if($leftVariant>count($firstArray)) $leftVariant = count($firstArray); $rightVariant = rand(1, 3); if($rightVariant>count($secondArray)) $rightVariant = count($secondArray); for($x=0;$x<$leftVariant;$x++) { array_push($leftNewCards, array_shift($firstArray)); } for($y=0;$y<$rightVariant;$y++) { array_push($rightNewCards, array_shift($secondArray)); } if($bottomFirst==0) { $newCardsArray = array_merge($leftNewCards, $rightNewCards); $bottomFirst = 1; } else { $newCardsArray = array_merge($rightNewCards, $leftNewCards); $bottomFirst = 0; } reset($newCardsArray); while(count($newCardsArray)) { array_push($cardsArray, array_shift($newCardsArray)); } } if(count($firstArray)) { while(count($firstArray)) array_push($cardsArray, array_shift($firstArray)); } if(count($secondArray)) { while(count($secondArray)) array_push($cardsArray, array_shift($secondArray)); } } } Hello all, I am newbie to php programming, any suggestions or advance greatly appreciated. I can't figure out how to output two array random numbers in ascending order. <?php $l_numbers = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47); $mega = range(1, 27); for($i = 0; $i <= 4; $i++) { $random1 = array_rand($l_numbers); echo "$random1 \n" ; } for($e = 1; $e <= 1; $e++) { $random2 = array_rand($mega); echo "<p> Mega number is $random2 </p>"; } echo "Winning numbers are $random1 and mega $random2"; ?> example: Winning numbers are 3,5,10,17,39 and mega 5. Hi
I would like to use regex in php for this array : $strings = array("words", "test" ,"1=1"); words and test is ok , but I want set 1=1 like $i=$i for any value (just numbers) 2=2 or 10=10 and .... So I already have this below...
$state = array ( Can those numbers be a range -- 1-16, 17-32, 33-48 and 49-64 -- without just listing them all?
Hello, I am fairly new to PHP and I found some php code to print an array of numbers that create a combination - however, its slams all the numbers together without a space or a comma separator. How can I separate the numbers in this array with the given code? Ive attached a photo of the code.
The Script:
<form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>"> <input type="text" name="hashtags" /> <input type="submit" name="submit" /> </form> <?php include("connect.php"); ?> <?php if(isset($_POST['submit'])){ $hashtags = explode(", ", $_POST['hashtags']); for($i= 0; $i < count($hashtags); $i++){ $tqs = "SELECT `id` FROM `hashtags_two` WHERE `hashtags` = '" . $hashtags[$i] . "'"; $tqr = mysqli_query($dbc, $tqs) or die(mysqli_error($dbc)); } $fetch_array = array(); while($row = mysqli_fetch_array($tqr)){ $fetch_array[] = $row['id']; } }Hey, sorry for these fundamental array questions lately, I am not going to ask much more when it comes to this. The hashtags come from the submit form (separated by commas) and get stored inside an array with the "explode()" function. The script should select the ID numbers of the hashtags from the table. With the script above only the last row gets inserted into $fetch_array inside the while loop. When the $fetch_array variable is printed on screen then only one ID number can be seen inside the array: Array ( [0] => 24 )How can I have the other ID numbers too? How do you generate a random number like say, 0 through 10? I want to select random sites from my database that have IDs 0 through 10. Hi there. I need a function that can generate 15 random numbers between (1,10) and then converts those numbers to Roman numbers. Anyone can help me? Hello........ Currently i am struggling with php script....... can any one tell me or guide me how to code Automatically generating random number with in the range of 19 to 90 and the number has to change with in 24 hours time duration......... It's urgent........ Thanks in advance..... Rooban.S Hi, What I'm trying to do is create a script that generates a random list of cars whenever it is run. I have a list of available cars in a MySQL database. I'm just not sure how to get php to generate a list of, say, 20 cars, that are randomly picked from the cars in the database. I may also want to add other statistics like MPG (also already in the database). Can anyone show me how to do this? And is it possible to format the returned data in a table or div format? Thanks in advance Unk hi i am trying to make a payroll calculator script that takes employee info, calculates pay, displays submitted info in a table, stores info in an array, and updates the array when new info is submitted. i have most of these accomplished, i am having trouble with the "store into an array, and update the array when new info is submitted" parts of the project. i am still not very fluent in php so there may be easier ways to achieve what i have so far. any pointers would be a great help, this part has got me stumped. I am having some problems getting a query correct. Basically I have two tables, one with listings and another with categories. In the listings table I have a column called shortdescription. I am trying to pull the shortdescription from the listings table with the query $shortdesc = array_shift(mysql_fetch_row(mysql_query("select shortdescription from links where category = '".$scat->id."' "))); The shortdecription display properly on the pages display the listings, however I am getting the following error on any pages that only display the parent categories Warning: array_shift() [function.array-shift]: The argument should be an array in /home/...path/file.php on line 1462 The listings id numbers begin at 75+ because the initial parent category id ends at 74. The query seems to be searching for listing ids below 75 and spitting out an error because it is not finding them. Any ideas on how to eliminate this error and/or stop the query from looking for non-existant data? Using curl_multi, I have loaded up 3 url's and then printed the array. 1) How can I also set a timestamp on output to let me know when each url was run? 2) How can I explode the array to only display the data and timestamp, excluding the text "Array ( =>", "[1] =>", "[2] =>" and " )"? Code <?php function multiRequest($data, $options = array()) { // array of curl handles $curly = array(); // data to be returned $result = array(); // multi handle $mh = curl_multi_init(); // loop through $data and create curl handles // then add them to the multi-handle foreach ($data as $id => $d) { $curly[$id] = curl_init(); $url = (is_array($d) && !empty($d['url'])) ? $d['url'] : $d; curl_setopt($curly[$id], CURLOPT_URL, $url); curl_setopt($curly[$id], CURLOPT_HEADER, 0); curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, 1); // post? if (is_array($d)) { if (!empty($d['post'])) { curl_setopt($curly[$id], CURLOPT_POST, 1); curl_setopt($curly[$id], CURLOPT_POSTFIELDS, $d['post']); } } // extra options? if (!empty($options)) { curl_setopt_array($curly[$id], $options); } curl_multi_add_handle($mh, $curly[$id]); } // execute the handles $running = null; do { curl_multi_exec($mh, $running); } while($running > 0); // get content and remove handles foreach($curly as $id => $c) { $result[$id] = curl_multi_getcontent($c); curl_multi_remove_handle($mh, $c); } // all done curl_multi_close($mh); return $result; } $data = array(array(),array()); $data[0]['url'] = 'http://search.yahooapis.com/VideoSearchService/V1/videoSearch?appid=YahooDemo&query=Pearl+Jam&output=json'; $data[1]['url'] = 'http://search.yahooapis.com/VideoSearchService/V1/videoSearch?appid=YahooDemo&query=Black+Eyed+Peas&output=json'; $data[2]['url'] = 'http://search.yahooapis.com/VideoSearchService/V1/videoSearch?appid=YahooDemo&query=Nirvana&output=json'; $r = multiRequest($data); print_r($r); ?> Output Array ( => Pearl Jam [1] => Black Eyed Peas [2] => Nirvana ) Preferred Output 01:00:01 Pearl Jam 01:00:02 Black Eyed Peas 01:00:03 Nirvana |