Jump to content

isbhenrylu

Members
  • Posts

    15
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Male
  • Location
    UK, Taiwan

isbhenrylu's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. ?? Don't quite get this sentence. Think part of it is missing? So you mean that when -1 or 0 is returned, the location of the two subarrays don't switch? And only when 1 is returned that the two are switched around?
  2. Great help! I'm almost there. Let me see if I could get it right soon. Sorry for all the fuss guys. $products = array (array( 'TIR', 'Tires', 100), array( 'OIL', 'Oil', 10), array( 'SPK', 'Spark Plugs', 4) ); function (compare ($x, $y){ if ($x[1] == $y[1]) { return 0; } else if ($x[1] < $y[1]) { return -1; } else { return 1; } } usort ($products, 'compare'); Keith can you see if what I'm saying about the process is correct? Greatly appreciated 1. usort() function passes the 1st and 2nd subarrays of $products to compare(), which is array( 'TIR', 'Tires', 100) and array( 'OIL', 'Oil', 10). 2. Because Tires > Oil, the value "1" is returned. And usort recognizes that and switches array( 'TIR', 'Tires', 100) with array( 'OIL', 'Oil', 10), so currently the array is $products = array (array( 'OIL', 'Oil', 10), array( 'TIR', 'Tires', 100), array( 'SPK', 'Spark Plugs', 4) ); 3. Then usort() passes the current 2nd and 3rd subarrays to compare(), which are array( 'TIR', 'Tires', 100) and array( 'SPK', 'Spark Plugs', 4). 4. Because Tires > Spark Plugs, the value "1" is returned. And usort recognizes that and switches array( 'TIR', 'Tires', 100) with array( 'SPK', 'Spark Plugs', 4), so finally the array is sorted as: $products = array (array( 'OIL', 'Oil', 10), array( 'SPK', 'Spark Plugs', 4), array( 'TIR', 'Tires', 100) ); And also another question. According to what you have said in your posts, when compare() returns "1" to usort(), usort() will recognize that and switch the two subarrays. So does that mean when "0" is returned the two subarrays don't change positions? If so, what about if "-1" is returned? How does usort() deal with that? Does it leave the two subarrays unchanged as well? If so, why do you need one if() to return 0 and another else if() to return -1 if both of them are going to yield the same result: Which is keep the positions of the two subarrays under comparison unchanged. Then shouldn't it already be adequate to sort the array if the compare() function only returns two values? (either 0 or 1, or -1 or 1) Thanx Henry
  3. (Didn't see KickStart's post when posting this. Will take a look at his post right now) thanks a lot play_ . Help greatly appreciated. Here's the bit in the php book that tries to explain the situation (though I have no clue what he's talking about) that might help. Source: "PHP and MySQL Web Development" by Luke Welling and Laura Thomson Hope this helps a little. thanks. Henry
  4. Really sorry guys but I'm quite new to PHP programming and what you said is getting me a bit confused. For the post by mjdamato, I don't get the array you just posted. Why does your array start with this? [0]=> I thought multidimensional arrays look something like this? $somevariable = array (array(), array(), array()); And also what does these mean? I think it's better if someone could explain how to sort multidimensional arrays using the function I posted in the first post because I'm more familiar with it. Here's what I DO get: 1. The array is: $products = array (array( 'TIR', 'Tires', 100), array( 'OIL', 'Oil', 10), array( 'SPK', 'Spark Plugs', 4) ); 2. That compare() is a self defined function with 2 variables and that usort is just a function that tells $products to be sorted by compare(). Here's what I don't get: 1. what do the two variables in compare() $x and $y stand for as they have no correlation to the array I'm trying to solve. I guess it's just a format, so what should be the values that I substitute into them? 2. What happens when it returns 0 or 1 or -1? I can't see how the returning of either of those 3 values would result in the array being sorted by the 2nd column (Tires, Oil, Spark Plugs). How are these values then passed on in the process to contribute to the array being sorted? 3. What would the whole sorting function look like in the end? Exactly like this one, or is there anything else that I add to it for it to be sorted? (don't need the outputting bit because I know how to do that) function (compare ($x, $y){ if ($x[1] == $y[1]) { return 0; } else if ($x[1] < $y[1]) { return -1; } else { return 1; } } usort ($products, 'compare'); Thanks a lot for the help
  5. thx play_ for the link. Took a look at it but still have no idea what is the process that resulted in the multidimensional array being sorted. I think my biggest problem is I don't understand what function (compare ($x, $y){ if ($x[1] == $y[1]) { return 0; } else if ($x[1] < $y[1]) { return -1; } else { return 1; } } is trying to do. How do how those values returned (0, 1, -1) contribute to the multidimensional array being solved? Thanks
  6. Hi guys, I can sort arrays already using the sort, ksort, or asort functions. But for multidimensional arrays, I can't do it. I looked up the php book by Luke Welling and Laura Thomson and here's their array : $products = array (array( 'TIR', 'Tires', 100), array( 'OIL', 'Oil', 10), array( 'SPK', 'Spark Plugs', 4) ); According to them, to sort this array into alphabetical order using the second column in the array (which consists of Tires, Oil, and Spark Plugs), you need a self defined function and usort. They say the code to sort it should contain these snippetes: function (compare ($x, $y){ if ($x[1] == $y[1]) { return 0; } else if ($x[1] < $y[1]) { return -1; } else { return 1; } } usort ($products, 'compare'); Problem is I still don't understand how to sort multidimensional arrays after looking at the example. Can someone please explain to me what their example is trying to do and how should the whole code to sort it look like? If their example seems confusing to you as well, you could ignore it and tell me how you would do to sort multidimensional arrays Thank you Henry
  7. Thanks for the help guys. really really appreicate it. Just one more dumb quesiton. For the crtl +space thing that you mentiond, it doesn't quite work for me. I typed: echo date( within php tags and then pressed crtl +space, but nothing showed up. Any ideas?
  8. I'm used to dreamweaver right now so if possible I want to stick to it. For people programming in dreamweaver, how do you guys this with the problems I mentioned? Is there a way around that or is there something I didn't turn on? Or is it just that all programmers remember all their functions by heart?
  9. Hi guys, I've being developing websites using xhtml and css before, and dreamweaver cs3 was great for that, but when I'm now programming with php and mysql, I found it was nearly no better than notepad. First, the auto-code completion when I enter </ for html doesn't work anymore as long as it's within the <?php ?> tags. It only shows the code hint, making it a lot longer to code if I want to include html stuff in there. eg. I enter <table></ inside <?php ?> tags and it doesn't automatically complete </table> for me anymore, like what it does when it's outside of <?php ?> tags. Also there is no effective code hint for php as well. eg. When I start typing a function, it only gives me the format of the function and not the codes available in which I could choose from and then simply press enter (like how in normal html when I type < it gives me all the available tags like <p>, <h1>...) . This essentially means I have to type out every php function completely by hand! I thought dreamweaver was MADE FOR programming. What's the problem. Can the above be solved or what is it. Because it really annoys me that I have to remember or look up every function every time! If dreamweaver cannot do some effective code hint, anyone know a better editor? Thanks.
  10. Hi guys, I've being developing websites using xhtml and css before, and dreamweaver cs3 was great for that, but when I'm now programming with php and mysql, I found it was near junk. First, the auto-code completion for html doesn't work anymore as long as it's within the <?php ?> tags. It only shows the code hint, making it a lot longer to code if I want to include html stuff in there. Also there is no effective code hint for php as well. eg. When I start typing a function, it only gives me the format of the function and not the codes available in which I could choose from and then simply press enter (like how in normal html when I type < it gives me all the available tags like <p>, <h1>...) . This essentially means I have to type out every php function completely by hand! I thought dreamweaver was MADE FOR programming. What's the problem. Can the above be solved or what is it. Because it really annoys me that I have to remember or look up every function every time! If dreamweaver cannot do some effective code hint, anyone know a better editor? Thanks.
  11. Sorry mate, but this still doesn't solve the problem. The results is still: NameId Name 1 NAME1 2 NAME2 3 NAME3 4 NAME4 5 NAME5 instead of what I want it to yield, which is suppose to be: NameId Name 1 Henry 2 Mary 3 Gary 4 Cherry 5 Berry Please help! thanks
  12. Hi guys, I've got a problem to do with the while() function. What I'm trying to do is to create a table of 2 columns, one column is user id and the other is user names. This is what I did: <table border="2px"> <tr> <th>NameId</th> <th>Name</th> </tr> <?php $c = 1; define ('NAME1', 'Henry'); define ('NAME2', 'Mary'); define ('NAME3', 'Gary'); define ('NAME4', 'Cherry'); define ('NAME5', 'Berry'); $i = 1; while ($c <= 5){ echo "<tr><td>" . $c . "</td>"; while ($i == $c){ echo "<td>" . NAME . $i . "</td>"; $i++; } echo "</td>"; $c++; } ?> </table> The user id part works fine, going from 1-5 for the 5 rows. But for the user name column, the result is NAME1, NAME2, NAME3, NAME4, NAME5 instead of Henry, Mary, Gary, Cherry, Berry. Can someone please tell me why echo "<td>" . NAME . $i . "</td>"; is interpreted literally as NAME1 instead of finding the constant NAME1 and returning the value "Henry", which was defined in the constant at the start of the script. Really appreciate if someone can show me how to fix this problem as I'm a newbie in programming, thanks!
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.