PHP - Array Building
Greetings freaks,
I am trying to figure out how to build an array for the following purpose, and have just been spinning my wheels as it's a very new topic to me. I have a database with a table and the following important rows: ID, Parent, and Name. In this table, Parents are given the value 0 for 'Parent' and a unique 'ID' & 'Name' - children are given all unique 'ID' and 'Name' with the value for Parent being 'ID' of the Parent they belong to. Example: ( as 'ID', 'Parent', 'Name') 1, 0, John & 2, 1, Jane / 3, 0, Mark & 4, 3, Mary & 5, 3, Mindy. As you can see, the second names in each example (the children) are connected to their parents by the 'Parent' row, whereas the first names (the parents) are not associated with anybody else. What I would like to do is build an array that associates every child to the parent ([Parent] => child1 ). Simple MySQL grants me an array for the parents: $parent_pull = mysql_query('SELECT * FROM `table` WHERE Parent = 0') while($row = mysql_fetch_array($parent_pull)){ $name[] = $row['Name']; $id[] = $row['ID']; } Now, I want to be able to use the $id[]s to build arrays WHERE Parent = $id[]. I have tried in loops with $id[$i] & $i++ to cycle the array values, but PHP tells me there mysql_fetch_array() expects parameter 1 to be resource, boolean given ... on line 32. $i = 0 while($i < 10){ $child_pull = mysql_query('SELECT * FROM `table` WHERE Parent = $id[$i] '); $child = mysql_fetch_array($child_pull); $child[] = $child['Name']; <-- Line 32 $i++; } How would I go about pulling all the children and associating them with their parents? Similar TutorialsHi All, I have an array like this where the cars are the first level of the array, and the owners are children of that first level: [Car 1] ----[Owner 1] [Car 2] ----[Owner 2] [Car 2] ----[Owner 3] [Car 2] ----[Owner 4] I want to convert it to: [Car 1] ----[Owner 1] [Car 2] ----[Owner 2] ----[Owner 3] ----[Owner 4] So I started making this script: $cars = array(); foreach($owners as $owner) { if(!in_array($owner['Car'], $cars)) { $cars[] = $owner['Car']; } echo array_search($owner['Car'], $cars, true); $cars[array_search($owner['Car'], $cars)]['Owner'][] = $owner['Owner']; } The result of this is the same as the beginning result, any ideas what could be causing that? I'm trying to convert a one dimensional array into a multidimensional array by grouping matching parts of the keys,
i.e. array('onetwothreefour'=>'value',
'onetwothreesix'=>'value2')
would become
array('onetwothree'=>array('four'=>'value',
'six'=>'value2'));
Essentially I'm trying to convert XML to YML and have a one dimensional array of the XML with index => value.
I've been staring at it for a while now and have tried a few recursive functions to build it though I've come to a few dead ends and would appreciate any words of wisdom if someone can spot the answer easier than I
Edited by joel24, 23 May 2014 - 09:51 AM. Hi all, I am trying to build a multidimensional array from values in a URL. For example: page.php?ind=123&loc=456&wt=789 needs to build the array like this: Code: [Select] Array ( [0] => Array ( [0] => 123 ) [1] => Array ( [0] => 456 ) [2] => Array ( [0] => 789 ) ) If one of these params is empty: page.php?ind=123&loc=&wt=789 I need the array to be Code: [Select] Array ( [0] => Array ( [0] => 123 ) [1] => Array ( [0] => 789 ) ) I am having some trouble wrapping my head around how to do this. I am using array_push to construct it, but i fear I may be missing something. There's a good change I am way off too . Any suggestions would be greatly appreciated. My code: <?php // get the id from the URL if (isset($_GET['ind'])) { $indId = $_GET['ind']; } if (isset($_GET['loc'])) { $locId = $_GET['loc']; } if (isset($_GET['wt'])) { $wtId = $_GET['wt']; } $searchArray = array(); if (!isset($_GET['ind'])) { empty($_GET['ind'); } else { array_push($searchArray, $ind); } if (!isset($_GET['loc'])) { empty($_GET['loc']); } else { array_push($searchArray, $loc); } if (isset($_GET['wt'])) { empty($_GET['wt']); } else { array_push($searchArray, $wt); } echo '<pre>'; print_r($searchArray); echo '</pre>'; ?> Hey guys, I'm not exactly sure what the term used for this is, but I'll try explaining it very thoroughly. My site I'm building is this: http://r-gaming.net/urban-rp/home/ On index.php file, I would like it so you if you're logged in (I have a logged in system already scripted), you're able to post current news. Something like this: http://ls-rp.com/ Would anyone willing to help me by linking a tutorial to code this method 'latest news' tool? Would be greatly appreciated. Crayon Hi guys, I have recently been asked by a freinds to turn his web-site in to a CMS web site and i was just wondering if implementing it in php is the write way to go about it. I have never realy build anything like a CMS so if you guys have tips for me that would be spot on. Hi people. I'm building a quiz with one correct answer out of possible 4. My problem is that after I choose a random answer (which is the correct one) I have problems with choosing random 3 wrong answers which are not the same as answer 1 (they all come from the same table). My first mysql query gives me back 1 row my second mysql query gives me back 15 row (there were 16 - 1 which is the correct one). How can take only 3 rows out of that second query and output them into variables that I can later echo into a form? Can you help me understand this problem or can you propose an alternative method to deal with this? Here are my two queries: Code: [Select] // Fetch random answer (correct) $query = "SELECT * FROM psychofarm_brand WHERE `id`=".$randomNumber; $result = mysql_query($query); $row = mysql_fetch_array($result); // Fetch wrong answer $query2 = "SELECT * FROM psychofarm_gen WHERE `id`!=".$randomNumber; $result2 = mysql_query($query2); $row2 = mysql_fetch_array($result2); I hope this is clear. Thanks Hi Guys I am trying to build a menu sing the code below, I can get the main headings, the problem arises when I try and produce the sub menus. Can someone please tell me where I am going wrong, or a better way of getting the desired result Code: [Select] <?php function submenu() { $submenu_sql=mysql_query("SELECT * FROM `tbl_menu` where `parent_menu_id` = '$menu_row[menu_id]'"); if(mysql_num_rows($submenu_sql)>0) { echo('<ul>'); while ($submenu_row=mysql_fetch_assoc($submenu_sql)) { echo('<li><a href="'.$submenu_row['link'].'">'.$submenu_row['menu_display'].'</a></li>'); } echo('</ul>'); }; } $menu_sql=mysql_query("SELECT * FROM `tbl_menu` where `parent_menu_id` = '0'"); if (mysql_num_rows($menu_sql)>0) { echo('<div id="accordian">'); while ($menu_row=mysql_fetch_assoc($menu_sql)) { echo('<div class="panel_container"> <h3><a href="'.$menu_row['link'].'">'.$menu_row['menu_display'].'</a></h3> <div class="panel_body">'); submenu(); echo('</div> </div>'); } echo('</div>'); } ?> I am getting the following error Notice: Undefined variable: menu_row in /home/sites/j-slink.co.uk/public_html/includes/menu.php on line 4 The desired result is to produce a menu with the following layout Code: [Select] <div id="accordion"> <div class="panel_container" id="panel1"> <h3 id="visible">Home</h3> <div id="panel1-body" class="panel_body"> </div> </div> <div class="panel_container" id="panel2"> <h3>Music</h3> <div id="panel2-body" class="panel_body"> <div> <ul> <li>Calling Card Mixing Tape</li> </ul> </div> </div> </div> <div class="panel_container" id="panel3"> <h3>Panel 3</h3> <div id="panel3-body" class="panel_body"> <div> This is the contents of this panel. </div> </div> </div> <div class="panel_container" id="panel4"> <h3>Panel 4</h3> <div id="panel4-body" class="panel_body"> <div> This is the contents of this panel. </div> </div> </div> <div class="panel_container" id="panel5"> <h3>Panel 5</h3> <div id="panel5-body" class="panel_body"> <div> This is the contents of this panel. </div> </div> </div> </div> Thanks in advance I was thinking how can I make a script that will build strings incrementing through each letter starting with "a" and ending with "zzzzzz" or what ever ending length I want such as 12. So if your were to output everything, you could find the words: "dog" "spider" "superman" "hippo" etc What would be the best way to do this, so I don't have to have a loop that is 500 blocks deep to show a word that is 500 letters example Quote a ... z aa ab ac ... az ba bb bc ... ... aaa aab aac If I use this code: $values = range(0, 10); echo "<select name=\"my_field\">\n"; foreach($values as $v) { echo "<option value=\"$v\">$v</option>\n"; }echo "</select>\n"; to generate a dropdown, it will offer options from 1 thru 10. Is there any way to make them increment by .50 (ie: 0.0, 0.5, 1.0, 1.5, 2.0, 2.5, etc.? Hey guys, I'm currently trying to make a tutorial website, and I'd like to have a fairly simple blog / news page that I can update with pictures and news stories. I would also like people to be able to comment on these postings. I'm pretty new to PHP but i made a simple blog using a php handler and a database table, but I was just wondering is there some 3rd party application I can use to manage my website with like users and articles and such without having to use their website layout? Like, programs such as WordPress and PHP-Fusion, Joomla etc, dont let you use your own html and css pages do they? I want to keep my website but have something purely in the background for me to manage the content and maybe like paste some script to my actual pages or whatever. Im just lost is all I need a little guidance =] Thanks a bunch. In php how could I build a .net application? So I have a source that I want to build in .net, but i'm not sure how... Hello, I would like to build a php search engine against a mysql database. I am looking for a tutorial but haven't been able to find any good ones. Any ideas of where I can find one? I need one where I can search with several words and if it has the stemmer.class.inc and so on would be great also. thanks, df Hello i try to explain my situation, I have a dynamic table so i need to build a dynamic radiobutton list and build a dynamic sql req. First of all i build my table with radio button in a while So the radio button is named rdYanViau ( in the html it's working no prob with that ) Quote $name = $row2['FirstName'].$row2['LastName']; Quote <input type="radio" name="rd<?php echo $name; ?>" value="1"> Quote <input type="radio" name="rd<?php echo $name; ?>" value="2"> And in the same while i build a portion of my dynamic sql like that Quote $valueDeclare .= $row2['FirstName'].$row2['LastName'].', '; So in my insert this variable give me the FieldName and in the same while i build my $_POST variable for my sql sentence Quote $valueRadio .= "'".'".$_POST['."'".'rd'.$row2['FirstName'].$row2['LastName']."'".']."'."', "; This value is '".$_POST['rdYanViau']."' After that i build my sql with those 2 variables, the problem is when i execute my sql insert where he's supose to insert my rdButton value ... it's insert the string '".$_POST['rdYanViau']."' who give me a 0 in the table, he's not getting the value of '".$_POST['rdYanViau']."' he's inserting the string in the table. Here's the echo of my sql sentence : Quote INSERT INTO sondage (idSondage, username, idMatch, YanViau, YanViau2, YanViau3) VALUES (NULL , 'admin', '1', '".$_POST['YanViau']."', '".$_POST['YanViau2']."', '".$_POST['YanViau3']."') my echo should look like : Quote INSERT INTO sondage (idSondage, username, idMatch, YanViau, YanViau2, YanViau3) VALUES (NULL , 'admin', '1', '1', '2', '1') How i can build this dynamic sentence and have the radiobutton value and not the string value ... I hope you can understand my problem. Yan. Here's my build a querty code... Code: [Select] $sql = "UPDATE $tablename SET DIST_PART_NUM = '$dist_part_num', DIST_PUB = '$dist_pub', MFR_PART_NUM = '$mfr_part_num', MFR_PUB = '$mfr_pub', ITEM_DESC = '$item_desc', ITEM_STD_DESC = '$item_std_desc', ITEM_COPY = '$item_copy', COST = '$cost', GP_MULT = '$gp_mult', ITEM_IMAGE = '$item_image', ITEM_URL = '$item_url', COUNTRY_OF_ORIG = '$country_of_orig', CATEGORY_ID = '$category_id', PICGROUP_KEY = '$picgroup_key', UPC_CODE = '$upc_code', LEADTIME = '$leadtime', UNITS = '$units', LENGTH = '$length', WIDTH = '$width', HEIGHT = '$height', WEIGHT = '$weight', FLAG_HAZARDOUS = '$flag_haz', FLAG_LTL = '$flag_ltl', FLAG_NON_RETURNABLE = '$flag_non', CAMPAIGN_KEY = '$campaign_key', "; foreach($atr as $key => $value) { $sql .= $value . "='" . addslashes($_POST[$value]) . "',"; } $sql2 = rtrim($sql,","); $sql2 .= " WHERE DIST_PART_NUM = '$partnumber' OR MFR_PART_NUM = '$partnumber'"; Now the query get's built 100% fine that's not the issue... my issue is that sine if the table fields are named something like #_of_shelves... and the # sign causes a MySQL error... You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 26 How can i allow for the # sign, or any other character that can cause issues like () . , * ~ etc.... i tried encompassing the field name with single quotes 'fieldname' and with those slanted ones `tablename` but that did not help. Any ideas? Thanks! I am building a shopping cart with PHP and working with a tutorial (http://jameshamilton.eu/content/simple-php-shopping-cart-tutorial) I am putting together the cart and there is some coding $sql = sprintf("SELECT name, description, price FROM php_shop_products WHERE id = %d;", $product_id); That i do not understand. Here it is "WHERE id = %d;" I have never seen this before and am wondering what it means or how to work with this syntax Hi, I was trying to build a chat room and tried the code given he http://www.codingmix.com/2010/12/privat ... mment-form but when i try to go to chat , sending is disabled and I cant chat Can anyone help? Also I want to create a room for chat like: based on a topic some more features like Adding a topic........and while chat choose the category like speaking for the topic, against or neutral and with option to post a point while chatting please help pooja_dubey12 Forum Newbie Posts: 3 Joined: Fri Jul 03, 2009 8:29 am Why is the proper way to call a class is to call a method at the same time? For example Code: [Select] $class = new SomeClass(); $class->some_method(); I understand why you would do this but lets say you just have a simple class that you pass some data. The class than processes the data in some way than returns it. It is my understanding if you were to make a class likes this. Code: [Select] class SomeClass{ function __construct($data){ //process data// return $data; } } That this code is considered an improper way of doing this because you should never return data from the __construct. So would this follow code be the proper way to handle this? Code: [Select] class SomeClass{ function __construct($data){ $this->some_method($data); } function some_method($data){ //process data// return $data; } } Any good explanation on the proper way to handle this would be greatly appreciated. When I am making simple classes to do a specific task it would seem more efficient not having to know about any particular method inside the class. This topic has been moved to Application Design. http://www.phpfreaks.com/forums/index.php?topic=352102.0 Hello,
I want to know first, how to start new pages or small windows
Next, how to temporarily store data and then delete upon exit of chat so that memory can be saved
How to embed objects like images (objects?)
How to have resizable or set the size of input field for the message
Show a sign like "person is typing" when other person is inputting data
I'll probably get yelled at, I'm trying to amass all the information I need
I'll research each subject as I accomplish a task
Right now I could really use a download script that saves a photo, gets url of photo and searches the document for "By Name" and cuts out / stores the Name part and then renames the file with an increment of one, instead I have to do 300 clicks with my mouse. = ineptitude or ignorance
That is theoretically the ideal case but I have to sift through photos, see which ones are good eg. face is covered by watermark
Thanks for any help
Edited by greenace92, 02 December 2014 - 07:52 PM. This topic has been moved to mod_rewrite. http://www.phpfreaks.com/forums/index.php?topic=348849.0 |