PHP - Two Questions About Array Indexes
I have this simple code below to pull back info from a mysql database and create an array. I have two questions...
1. The items in this array will automatically have indexes of and [1] and [2] and so on. If I wanted the first index to be [1] instead of , how could I do that? In other words, I want the first item in the array to have an index of [1] (like regular counting) rather than (programming counting). 2. Each 'answer' actually has its own 'answerid' in the database, which I am bringing back as well with the query. If I wanted those answerids to be the indexes, how would I adjust the code below? Code: [Select] $sql = "SELECT answer, answerid FROM answers WHERE quizid = $quizid"; $createanswerarray = mysql_query($sql, $connection); if (!$createanswerarray) { die("Database query failed: " . mysql_error()); } else { $_SESSION['answers']=mysql_fetch_array($createanswerarray); } } else { } Similar TutorialsI've got a few arrays as follows: Code: [Select] $array1 = ('A', 54, 'a', 2); $array2 = (, 67, 'b',); $array3 = ('R', 54,,); $array4 = (, 36, 'b'); $array5 = (, 36, 'a',); $array6 = ('FX', 119, 's', 4); $array7 = ('R', 3, 'c',); $superarray = array($array1, $array2, $array3, $array4, $array5, $array6, $array7); Now, I've got a piece of code that sorts $superarray by index[1] of the sub-arrays. The code looks like this: Code: [Select] function sortmulti ($array, $index, $order, $natsort=FALSE, $case_sensitive=FALSE) { if(is_array($array) && count($array)>0) { foreach(array_keys($array) as $key) $temp[$key]=$array[$key][$index]; if(!$natsort) { if ($order=='asc') asort($temp); else arsort($temp); } else { if ($case_sensitive===true) natsort($temp); else natcasesort($temp); if($order!='asc') $temp=array_reverse($temp,TRUE); } foreach(array_keys($temp) as $key) if (is_numeric($key)) $sorted[]=$array[$key]; else $sorted[$key]=$array[$key]; return $sorted; } return $sorted; } $sortedarray = sortmulti($superarray, 1, ASC, $natsort=FALSE, $case_sensitive=FALSE) The output is this: R3c 36b 36a A54a2 R54 67b FX119s4 This is great because it goes 3 -> 36 -> 36 -> 54 -> 54 -> 67 -> 119. However, the problem, if you look, is this part: 36b 36a Its sorted everything according to index[1] of the sub-arrays, but now I would like to sort by index[2], then index[3] as well. I'd like the output to look like this: R3c 36a 36b A54a2 R54 67b FX119s4 Any ideas? Because this is way over my head ... Howdy, I am new to SEO. Could you please help me? 1. I like to post programming tutorials to both my website & various programming forums. Is that going to screw up my website's ranking in Google? 2. My editor of my history website who sometimes posts essays there also posts them in some history forums. Is that bad for SEO? 3. I made a Facebook page for my history website. It says there "Promote your page" basically you pay $5 for around 100 likes. Has anyone tried that? Does it work? Because $5 seems like little money for additional 100 likes which will increase the traffic considerably. Thank you so much for the help! I've been coding PHP for some time and would consider myself to be at an intermediate level. I can write code to do what I need but it's probably not the best way to do it. I rarely see any code that I am not able to read, understand, or follow. I've created modifications for everything from vBulletin, WordPress, Kayako Support Suit, Magento, and more. However, I've never really built a strong understanding around OOP. For example, let say you have the following classes: _main - db - admin - - modules - - - dashboard How would you share the db connection with the dashboard class? I've been trying to read up on Dependency Injections and Singletons but I haven't found an article that has explained it on a level that I can understand. I get a feeling that most people who use OOP in PHP have a background in Java or C++ and are much more familiar with everything. Could someone please explain this to me in simple terms or link me to an extremely well explained article that I'd be able to understand without a background in computer science? Thanks Hi. I'll like to ask few questions about PHP, as I think they are related to it.
I've came across some webpages, what I've spotted is that a webpage displays content but each "page" has different argument and there is no filename.
For example:
"http://www.website.com/?home" is home-like webpage, by changing "/?home" to "/?anotherpage" land me on some other webpage on their website and so on. My question is how is it done? Is it done from PHP?
Another question I wanted to ask is.. I went on InvisionPower.Board forum (such as this PHP Freaks ). How to force "folders" to be displayed as "files"?
For example:
"http://forums.phpfre...ks-on-facebook/" which links to a thread.
Thanks in advance
Hello everyone, I have been doing web developing for a little while and just recently decided to make the leap to developing standalone applications. I started learning C++ and JAVA for this purpose, but quickly learned that PHP can also be used to this end, and since I am quite familiar with PHP, I thought it would make sense to start with PHP GTK. But before I jump right in, I have a few questions that I would greatly appreciate some answers for: Does PHP have any significant advantages/disadvantages over lower level languages such as C++ ? I would imagine that PHP being originally designed for web programming would be less suited for stand alones. I'm a little confused as to whether the GTK is a graphical user interphase software, the likes of QT and Netbeans, or is it a markup language like HTML, where the widgets are generated with text commands? Please I need a little clarification on that. Also are there any other tools that need to be downloaded to get started besides the GTK? Finally, am I supposed to learn OOP PHP to get going or is traditional procedural PHP sufficient? Answers to any or all of the above questions and any other advice would be highly appreciated. Thanks. This topic has been moved to mod_rewrite. http://www.phpfreaks.com/forums/index.php?topic=348665.0 I've been looking for Curl tutorials and haven't really found any good ones. Can Curl take text from a website and store it in a variable? If so, can I get an example? If not, could someone please redirect me towards something it can? Example: A site has a list of items you've earned on a game, you want to store them and output them on your site. If this isn't clear enough please let me know. I am trying to create a simple form for inserting to a database table. It seems the data from the form are passed through the "insert" script, but nothing is added to the table. My question is: what is required to insert new data to a table? must all fields have a value for the new data to be added? Some questions about PHP Constants... 1.) What make a Constant "come to life"? 2.) What is the life of a Constant? 3.) Can you define a Constant in "file_1.php" and use the Constant in "file_2.php" and "file_3.php"?? Debbie This topic has been moved to Third Party PHP Scripts. http://www.phpfreaks.com/forums/index.php?topic=348384.0 Hey again, I hope you guys don't get annoyed with my noob questions. I was reading the guide here on the site about OOP, and on the section related to inheritance it makes a "Dog" class, after giving it an attribute and a method it then creates the "Animal" class using "extends" and expands on the methods. Then after that here is the paragraph I'm looking at now. "Class Animal is unaware of the fact that it is being extended; there are no references to Dog whatsoever. Say Animal extended another class called LifeForm, and I instantiated Animal, only methods and properties of Animal and LifeForm would be included in the object." To me, this sounds like if I were to create this "LifeForm" class by further extending the "Animal" class, I would not be able to access any of the attributes and methods that were in "Dog," only the ones that were in Animal and any that I put in LifeForm itself. But, I was playing with the code and I can call an attribute from Dog in LifeForm. So, am I reading the post wrong or am I coding wrong? Code: (php) [Select] <?php //Class Dog// class Dog{ public $hungry = 'Hell yeah!'; public $test = 'Affirmative!'; function eat($food){ $this->hungry = 'not so much.'; } } //Class Animal// class Animal extends Dog{ function eat($food){ if($food === 'cookie'){ $this->hungry = 'not so much'; }else{ echo 'Barf! I only like cookies!'; } } } //Class LifeForm// class LifeForm extends Animal{ function eat($food){ } } //Program Variables// $dog = new Dog; $LifeForm = new LifeForm; //Program Code// echo $LifeForm->test; ?> My assumption was that I am able to call that attribute because of the 'public' in front of it, but all the attributes in the examples are also public. Thanks guys! I basically have a picture uploading system for users. I have two questions: 1) What CHMOD should I use for the folders that are there for uploading files to? Currently it is 755, but I want it to be accessible and safe. 2) When I use the standard mkdir() function to create folders in my main parent folder, the folders don't get created. Is this because my parent folder is CHMOD 755? Thanks Hello, I'm fairly new to PHP, and I'm looking to create an online catalog for a furniture/appliance store. I'm wondering the best way to go about this. I'm looking at SQLMaestro's "PHP Generator for MySQL" http://www.sqlmaestro.com/products/mysql/phpgenerator/ ... But, I'm curious if you guys can point me in the right direction. I've never created a database before, and I want to do it the way that makes the most sense... Any advice would be appreciated!! Good day, I have 2 questions about that. Here is the context. I have a list of items that i query from a database and insert in a table. The last field of the table is a input box to typpe in the quantity ("qty"). My first question, how can I associate the inputbox to product_id from the database for that item. Code: [Select] //database connecting is working <? while($row = mysql_fetch_array($result)) { echo "<tr><td>" . $row['product_code'] . "</td><td>" . $row['product_name'] . "</td><td><input type='text' maxlength = '3' value='0'></td></tr>"; } And my second question (any tutorial reference) about how to select only the items that the qty is not = 0 and pass it to another page either by sessions or other means. Thank you Hi all, I my hunt for better coding i thought i look at some opensource CMS systems and one thing I saw in the index.php of the joomla cms was this. define('JPATH_BASE', dirname(__FILE__) ); define( 'DS', DIRECTORY_SEPARATOR ); require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' ); require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' ); Are they using DS to make it work on any platform instead of using a / or a \ ? Also they define JPATH_BASE, now they use dirname(__FILE__) for this. I read that __FILE__ tells from where the script is running. But in that same guide they gave as example dirname(dirname((__FILE__)) instead of only 1 time dirname() That was this website btw: http://www.phpguru.org/php-application-structure can anyone tell why i would use dirname 2 times and not 1 or 3 or 4? -edit: OK that last question don't needs an answer, i just put loads of dirname() around it and it seems it's moving up in the file system. Leaves my first still open Hello everyone, I am a hobbyist with programming. I have been playing with Php, sql, html for several years. I am building a program to organize my club. I am starting out fairly simple and hope in a year for it to be powerful. If anyone can point in the right direction to find out how I can do several things I can get my first version done this week maybe. Here are the things I am trying to accomplish. 1.) a page that I can view records with a First record, previous Record, next Record, last record buttons. The page will pull the information from a table or tables out of my mysql database. I would like to have an update button so I can update records as I go through them and to be able to search buy typing in first name or last name. I know there are functions that will help me and I have been trying to figure out what classes on php.net 2.) Last question for now. All my pages that deal with mysql I have included all the connection information. I was thinking about making a class and add using a class on the pages to open the connection. Not sure if this would be easy, or the best way. Thanks in advance. I am new and I am not looking for the exact code just place to start. I really want to learn php and sql to be able to build my own applications and fix them. What I want is to let visitors select a county they want to search in. And when the choice is made - all ads, news, info that is shown is from the chosen county. What I'm trying to say here, is that when a location is set, visitors don't need to set the location again. But they can change it all the time. I belive this is done by setting a cookie with PHP. But don't know how. Can anyone point me in the right direction? 1. If I start a new project, should I use Laravel 5.0(dev) or 4.2?
2. How easy is it to upgrade from between major version like that?
3. Any design recommendations for daily with authentication and accounts?
|