PHP Array help...

Ok,

Firstly hello I’m a 40 year old trying to learn php and i’ve never been great at reading dyslexic but I’ve become a fan of php and want to learn more so I’ve been tweaking some code and reading more. I do better at seeing how its done by others showing me and understanding it that way than reading as its a nightmare for me.

anyway Array’s I struggle with and I have a cell in a table that is done like this…

0|6|0

I need to get the 6 (CATID) or what ever the second no is into a string so that I can use the answer to probe another table

[php] $query = “SELECT active FROM category WHERE CATID = '”.$string from previous query.";
$active = @mysql($query);[/php]

I have been scratching my head and I’m sure that its a simple few scripts but I seem to go blank at working this out at times…I’m going wrong…

Your patience and help is greatly appreciated. :slight_smile:

Change to this and post the result here:

[php]echo ‘

’;
var_dump($resultOfLastQuery);
die();
$query = “SELECT active FROM category WHERE CATID = '”.$string from previous query.";
$active = @mysql($query);[/php]
you have to change $resultOfLastQuery to reflect your local name of the variable holding the result(s) of the last query

Hello thank you for your help maybe didn’t explain myself well enough…

how do I extract the no 6 out of the cell that reads 0|6|0

I get the impression I have to select it and explode it - sounds painful and assign the parts of the array…

How do you do this once I have the first part then I can try the next part…

Many Thanks

Do you use a proper editor (IDE) like Netbeans? If not I would suggest to try it :slight_smile: if you (in an IDE) type “ex”, it will auto-suggest functions and variables. Selecting “explode” will give you the phpdoc for that function.

You can also use the (excellent) PHP.net manual
Google search: php explode
first hit: http://php.net/manual/en/function.explode.php

array explode ( string $delimiter , string $string [, int $limit ] )

Returns an array of strings, each of which is a substring of string formed by splitting it on boundaries formed by the string delimiter.

[php]$string = ‘0|6|0’;
$array = explode(’|’, $string);

echo ‘

’;
var_dump($array);[/php]

Output:

array(3) { [0] => string(1) "0" [1] => string(1) "6" [2] => string(1) "0" }

Out of curiosity: what is the reason you have values like “0|6|0”?

Hello and welcome :wink:

As a sidenote - as you are just beginning this is a perfect time to let you know a couple of things:

@mysql($query);

There are two issues with the command above, let’s tackle them one at a time.

  1. ‘mysql’ <-- This is old, very old. It has been depreciated and will be removed at some point in the (near?) future, at that point ALL of your scripts will stop working! Not good!
    Use MySqli or PDO instead.
    see here for more details.

  2. ‘@’ <-- If you want to learn, definitely don’t do this! This suppresses errors and often leaves you with a blank screen and wondering what the hell happened.
    see here for more details.

Hope that helps,
Red :wink:

^- What he said

I just wrote up a tutorial that may get you started using PDO.
http://www.phphelp.com/forum/the-occasional-tutorial/using-php-with-databases-(pdo-tutorial)

Thanks for the +1 Jim :wink:

Ok apologies for the later reply was out all day at a fayre yesterday in the wilds of northumberland with the family.

anyway I did some reading and redoing…

This is my code…

[php]$sql = “SELECT CAT FROM category WHERE CATID = '”.$previousqueryresult."’ LIMIT 1";
$rs = $conn->execute($sql);
$ccarray = $rs->getrows();

$testresult = $ccarray[‘0’][‘column2’];

echo ‘

’;
var_dump($testresult);
die();

[/php]

Output was

array(1) { [0]=> array(2) { [0]=> string(1) "1" ["column2"]=> string(1) "1" } }

so its getting there the column 2 = 1 if I change it via the db to 3 the result changes to 3 so it looks ok so far…

The next hurdle is to extract that column 2 field into a string

so that I can then say

[php]if (testresult = 1) {

echo = “hello world”;

}else{

Try this…

}
[/php]

I just assumed that if I said

[php]$testresult = $ccarray[‘0’][‘column2’];[/php]

It would work but on trying it its seems to have a glitch because the cell above is set to a 0 and it seems to hold the previous result do you need to empty the array or something so that when its called again it starts afresh…

Being able to see the printed array is cool - less hunting in the dark. I realise this is most likely easy peasy for you guys but I felt like I was breaking new ground when seeing it…

Off to read your tips on the Mysqli etc

[php]
if (testresult = 1) {

echo = “hello world”;

}else{

Try this…

}[/php]

Few things with this code that may be throwing you a little…
[php]if (testresult = 1) [/php]

  1. testresult should have a variable sign in front of it otherwise php will assume it is a constant name and look for that.
  2. a single equals symbol assigns a value, to check for matches use a double equals like so ==
    so that line should be:
    [php]if($testresult == 1)[/php]
  3. echo doesn’t need an equals sign. so that line would be:
    [php]echo “hello world”;[/php]

Next, you say it is returning the value 0(zero).
When doing true false comparisons, don’t get confused between 1=yes or 0=no (boolean values)

Finally, when using arrays like so:
[php] $testresult = $ccarray[‘0’][‘column2’];[/php]
the position in the array (the number) should not be quoted. So that line should look like this:
[php] $testresult = $ccarray[0][‘column2’];[/php]

I appreciate it’s a lot to take in, but getting on top of these now will save you hours of headaches later.

Hope that helps,
Red :wink:

Sponsor our Newsletter | Privacy Policy | Terms of Service