Jump to content

Recommended Posts

1) What is the function to cut an array. I am looking to slice out the first x number of array items. I cannot find the function to do this. The function I used before removed the items then reordered the array as 1,2,3 etc

 

2) What is the difference between -> and [] when it comes to getting data from a database?

Link to comment
https://forums.phpfreaks.com/topic/108000-2-quick-questions/
Share on other sites

to answer question 2 (seeing Crayon Violent answered 1, and did a good job of answering 2 as well....) the way I think you were asking it:

-> and [] are a matter of preference. Are you more object-oriented of a programmer (->) or, are you more of a code as you go ( [] )? I'm more  of a code as you go guy.

Link to comment
https://forums.phpfreaks.com/topic/108000-2-quick-questions/#findComment-553543
Share on other sites

-> is an object pointer.  If you have a class say

 

class blah {
   function printSomething ($something) {
      echo $something;
   } // end printSomething
} // end class

$ps = new blah();
$ps->printSomething("some message here");

 

printSomething is a method inside class blah.  First I made an object called $ps.  Then call the method printSomething by taking my $ps object and pointing to it.  Many people use object oriented programming (OOP) to handle database interaction.  They would have a method for connecting to the database, making queries to the database, handling/formatting the results, etc... for example, someone could have an object called $db and to connect to the db, they'd point to a method inside some class they made $db->connect();  and the method would run the commands to connect.  Or they'd maybe do $db->dumpTable(); and maybe the function would do a generic select * from table and list everything or something.  It's supposed to be an easier way to code, because lots of people feel that the more separation of code and design the better.  They would rather be able to have

 

html code here

html code here

one little php command here

html code here

html code here

 

than

 

html code here

html code here

lots of php commands

lots of php commands

more php commands

html code here

html code here

 

which is kind of a valid argument, especially when you have a whole lot of code and you've got more than one person working on a site and one dude focuses on the design and one on the code and maybe that designer doesn't know much about coding, so that makes it easier for him.

 

[] is used to denote positions in an array.  If you have an array, say:

$blah = array('a','b','c');
echo $blah[1];

 

$blah is an array. I am echoing the 2nd position in the array (position one starts at 0, not 1). An array is pretty much an easy way to group together similar data.  For instance, abc are letters in the alphabet.  Instead of having a variable for each one

 

$a = "a";

$b = "b";

$c = "c";

 

You can make an array to hold all of them; a "super" variable with more than one slot, if you will. 

 

variable = something that has 1 slot for info.

array - something that has lots of slots for lots of info.

 

Certain functions for retrieving and handling data from your database return you an array of information. Consider this:

 

$sql = "select somecolumn from sometable";
$result = mysql_query($sql);
while ($list = mysql_fetch_row($result)) {
   echo $list[0] . "<br />";
}

 

$list is an array. mysql_fetch_row only returns one slot for this array, so it kinda seems like overkill to have an array with just one position.  we use $list[0] because again, we start at 0 not 1.  This is called a numerical array.  You can label the array positions so you can more easily remember what is supposed to be there.  Those are called associative arrays, because you are associating a name with the position. If we had used "mysql_fetch_assoc" instead of "_row" we would have echoed $list['somecolumn'] instead of $list[0].  $list['somecolumn'] still signifies the first (and in this case, only) position in the array, but mysql_fetch_assoc labeled the position 'somecolumn' - the same name as our column name we queried for - so that we can remember what kind of data we have. 

 

I have a sneaking suspicion that you probably already knew everything I've said here; I'm not trying to talk down to you.  I was just trying to (further) explain how, although -> and [] are seen in things like db handling, they aren't directly associated with it.

Link to comment
https://forums.phpfreaks.com/topic/108000-2-quick-questions/#findComment-553577
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.