News:

SMF 2.1.4 has been released! Take it for a spin! Read more.

Main Menu

php Array Help

Started by littleone, January 08, 2007, 03:13:41 PM

Previous topic - Next topic

littleone

Ok i have a little bit of Java experience and I am using php.net to read up on stuff and I can see whats wrong (i think) but I cant figure out how to fix it.

My Database has the following:
us.png,ca.png

I want it to return it in an array with us.png being the first thing and then ca.png being another so that when it loops through the array it prints out us.png and then the next loop would print out ca.png but instead its doing us.png,ca.png (its just removing the , and putting it all in the first place but i guess in the array printing out it readds the ,)

// Is there a flag set?
if ($row_flag['shop_Flag'] !== '')
{
// Output the text at the start
echo 'Icons: ';

// Get the flags into an array
$flags = explode(',', $row_flag['shop_Flag']);
// Loop through the array
foreach ($flags as $flag)
{
// Is the flag real (not a blank string)?
if ($flag != '')
echo '<img src="', $boardurl, '/Sources/shop/flag_images/', $row_flag['shop_Flag'], '">';
}


Let me add that it does correctly print out the # of times is supposed to.  So it prints out the image 2 times but instead of image 1 being us.png and image 2 being ca.png it prints out 2 images with us.png,ca.png. So if I add another like az.png is prints 3 images with us.png,us.png,az.png

Thanks ;)

Thantos

I would think that echo '<img src="', $boardurl, '/Sources/shop/flag_images/', $row_flag['shop_Flag'], '">'; should be echo '<img src="', $boardurl, '/Sources/shop/flag_images/', $flag, '">';

littleone

Quote from: Thantos on January 08, 2007, 03:24:12 PM
I would think that echo '<img src="', $boardurl, '/Sources/shop/flag_images/', $row_flag['shop_Flag'], '">'; should be echo '<img src="', $boardurl, '/Sources/shop/flag_images/', $flag, '">';

Indeed that did it ;)  I appreciate your help on that.  I'll be posting a database problem question in a minute lol.

That was fast and greatly appreciate it ;)

littleone

function onUse() {
        global $db_prefix, $ID_MEMBER;
       
        /*
        $result = db_query("UPDATE {$db_prefix}members
                            SET shop_Flag = '{$_POST['flag']}'
                            WHERE ID_MEMBER = {$ID_MEMBER}",
                            __FILE__, __LINE__);
        */
        $result = db_query("UPDATE {$db_prefix}members
                            SET shop_Flag = shop_flag + ',{$_POST['flag']}'
                            WHERE ID_MEMBER = {$ID_MEMBER}",
                            __FILE__, __LINE__);

        return "Successfully added Icon {$_POST['flag']} to your Profile Icons!";
    }

}


I left the old version that worked in tact but quoted it out.  It only stores the one value and each time a new "icon" is purchased it overrides the old.  Well I want it to add it as u can see.  so if I have us.png there and I want to add ca.png it should read like us.png,ca.png in the database but what its actually doing whether is the first, second, ect purchases its not putting anything of the sort in the Database its actualling putting in a 0, and then thats what it prints out to is that 0.

littleone

Quote from: littleone on January 08, 2007, 03:39:57 PM
function onUse() {
        global $db_prefix, $ID_MEMBER;
       
        /*
        $result = db_query("UPDATE {$db_prefix}members
                            SET shop_Flag = '{$_POST['flag']}'
                            WHERE ID_MEMBER = {$ID_MEMBER}",
                            __FILE__, __LINE__);
        */
        $result = db_query("UPDATE {$db_prefix}members
                            SET shop_Flag = shop_flag + ',{$_POST['flag']}'
                            WHERE ID_MEMBER = {$ID_MEMBER}",
                            __FILE__, __LINE__);

        return "Successfully added Icon {$_POST['flag']} to your Profile Icons!";
    }

}


I left the old version that worked in tact but quoted it out.  It only stores the one value and each time a new "icon" is purchased it overrides the old.  Well I want it to add it as u can see.  so if I have us.png there and I want to add ca.png it should read like us.png,ca.png in the database but what its actually doing whether is the first, second, ect purchases its not putting anything of the sort in the Database its actualling putting in a 0, and then thats what it prints out to is that 0.

Sorry my back to back posts might have obscured the "follow up" question I had.  Thanks in advance for the solution ;)

Daniel15

Sorry, these errors were probably my fault (the code I PM'ed to you) :P

I think I did this wrong:

        $result = db_query("UPDATE {$db_prefix}members
                            SET shop_Flag = shop_Flag + ',{$_POST['flag']}'
                            WHERE ID_MEMBER = {$ID_MEMBER}",
                            __FILE__, __LINE__);

It should be:

        $result = db_query("UPDATE {$db_prefix}members
                            SET shop_Flag = CONCAT(shop_Flag, ',{$_POST['flag']}')
                            WHERE ID_MEMBER = {$ID_MEMBER}",
                            __FILE__, __LINE__);
Daniel15, former Customisation team member, resigned due to lack of time. I still love everyone here :D.
Go to smfshop.com for SMFshop support, do NOT email or PM me!

littleone

ok ill give that a try.  I swear I tried looking up concatination cause i know how it works in Java but I couldnt find it on php.net.  Probably was how i was trying to look it up.  None the less thanks for the help ;)

Daniel15

This is concatenation in MySQL, not in PHP. Anything in a db_query(".....") is a MySQL query. To concatenate in MySQL, you need to use the CONCAT function (as documented at http://dev.mysql.com/doc/refman/5.0/en/string-functions.html)

Concatenation in PHP is quite simple:

$test = 'Hello';
$test .= ' world!';

Daniel15, former Customisation team member, resigned due to lack of time. I still love everyone here :D.
Go to smfshop.com for SMFshop support, do NOT email or PM me!

Advertisement: