Customizing SMF > SMF Coding Discussion

array_values problem

(1/5) > >>

The Wizard:
Hello:

I can't seem to get the last function to work on the script I'm working on.

when I do print_r($row) I get -


--- Quote ---Array
(
    [wizard] => image1,image2,image3,image4,image5,image6,image7,image8,image9,image12
)
1
--- End quote ---

and when I do print_r($row) after -


--- Code: ---$row = array_values(array_diff($row,array($remove)));
--- End code ---

I get this -


--- Quote ---Array
(
    [0] => image1,image2,image3,image4,image5,image6,image7,image8,image9,image12
)
1
--- End quote ---

Here is the full code I have been working on -


--- Code: ---<?php

require_once('SSI.php');

    global $smcFunc, $db_prefix, $user_info, $boardurl;
    
// Get the user's information from the database

     $result = $smcFunc['db_query']('', "
 SELECT wizard
 FROM {db_prefix}members
     WHERE id_member = {int:current_member}
     LIMIT 1",
 array(
 'current_member' => $user_info['id'],
    )
);
 
    // creates a array
    while ($row = $smcFunc['db_fetch_assoc']($result)) {

    // Cut the string up into parts

$wizard = explode(',', $row['wizard']);
        }

// Simple Text Intro

    echo '<center><h3>SMFShop Item Gallery Remover<br>By: The Wizard<br>Version 2.0</h3></center>'; 

echo '<center><h3>Click on a item in your gallery to remove</h3></center>';

// Show Users Items in a Gallery
         
        $i = 0;

echo '<center><table><tr>';
foreach($wizard as $image)
{

// Ignore Items the user does not have

    if (empty($image))
continue;
$i++;
 
// Display Items the user has
 
echo '<td><a href="?run=first&a='.$image.'"><img src="', $boardurl, '/Sources/shop/wizards_item_image_gallery/small/', $image, '.png"></a></td>';

// To change the colums displayed across just change $wiz to the number you need to display

$wiz = 6;

if($i % $wiz == 0)
echo '</tr><tr>';
}

echo '</tr></table></center>';

// On Click - link to function 

    if (isset($_GET['run'])) $linkchoice=$_GET['run'];

    else $linkchoice='';

        switch($linkchoice){

        case 'first' :
                myFirst();
                break;

case 'second' :
                mySecond();
                break;
}

    // functions 

        function myFirst()

{
    
    global $boardurl;

// Get Variable from URL
        
$image = $_GET['a'];

// Simple Text Intro

echo '<center><h4>You have chosen the following item to remove</h4></center>';

// Show image that was clicked on

    echo '<center><img src="', $boardurl, '/Sources/shop/wizards_item_image_gallery/small/', $image, '.png"></center>';

// Simple Text Warning for idiot users

echo '<center><h4>Are you sure?<br>Once you click below the item will be removed forever.<br>If you want the item again you will have to buy it all over. </h4></center>';

// Link to remove item

        echo '<center><a href="?run=second&a='.$image.'"><h2>REMOVE ITEM</h2></a></center>'; 

}

// Removes Image name form array

    function mySecond()

{

global $smcFunc, $db_prefix, $user_info;

// Get the user's information from the database

     $doomsday = $smcFunc['db_query']('', "
 SELECT wizard
 FROM {db_prefix}members
     WHERE id_member = {int:current_member}
     LIMIT 1",
 array(
 'current_member' => $user_info['id'],
    )
);

$row = $smcFunc['db_fetch_assoc']($doomsday);

        $remove = $image;

echo '<pre>', print_r($row), '</pre>';

$row = array_values(array_diff($row,array($remove)));

echo '<pre>', print_r($row), '</pre>';

    // Save all the orginal image names Except the image name that was removed

    //updateMemberData($user_info['id'], array('wizard' => $row));

        //return '<br><center><h2>The item has been removed.</h2></center>'; 

    }

?>
--- End code ---

Hopefully someone out there can help me solve this problem.

Thanks for all your support and help.

Wiz

Suki:
So, what exactly are you trying to do here?  whats do $remove do? where is ti come?

$row, as far as I can see, is still a string, you need to explode it, to convert ti to an array, than you can use array_values() and all kind of array functions as long as its actually an array.

The Wizard:
There is a column in the database called wizard. In this column are the names of images - example - image1,image2,image3,image4.
This script is a removal script. Once it all works you can see a gallery of the images - click on a image and then if you still want to proceed you can remove that single image name form the column leaving all the other images names in the column.

The last function is the actual removal of a single image name form the column.

I did leave out a get statment in the function and so the function should be -


--- Code: ---// Removes Image name form array

    function mySecond()

{

global $smcFunc, $db_prefix, $user_info;

// Get the user's information from the database

     $doomsday = $smcFunc['db_query']('', "
SELECT wizard
FROM {db_prefix}members
     WHERE id_member = {int:current_member}
     LIMIT 1",
array(
'current_member' => $user_info['id'],
    )
);

$row = $smcFunc['db_fetch_assoc']($doomsday);

// Get Variable from URL
       
$image = $_GET['b'];

$remove = $image;

echo '<pre>', print_r($row), '</pre>';

$row = array_values(array_diff($row,array($remove)));

echo '<pre>', print_r($row), '</pre>';

    // Save all the orginal image names Except the image name that was removed

    //updateMemberData($user_info['id'], array('wizard' => $row));

        //return '<br><center><h2>The item has been removed.</h2></center>';

    }
--- End code ---

Sorry about the missing get statment. Hopefully this helps.

Wiz



Suki:
Yes, I now you have a string with something like 1,2,3,4,5, still, you need to actually explode it before you can use it as an array:

$array = explode(',', $string);

the $array will actually be an array, you can now use whatever function you want on it.

The Wizard:
Ha I got it! and here is the proof -


--- Code: --- // Removes Image name form array

    function mySecond()

{

global $smcFunc, $db_prefix, $user_info, $boardurl;
   
// Get the user's information from the database

     $result = $smcFunc['db_query']('', "
SELECT wizard
FROM {db_prefix}members
     WHERE id_member = {int:current_member}
     LIMIT 1",
array(
'current_member' => $user_info['id'],
    )
);

    // creates a array
    while ($row = $smcFunc['db_fetch_assoc']($result)) {

    // Cut the string up into parts

$wizard = explode(',', $row['wizard']);
        }

// Get Variable from URL
       
$image = $_GET['b'];

//$remove = $image;

$row = array_values(array_diff($wizard,array($image)));

// Turn String into Array

$pop = implode(',', $row);

    // Save all the orginal image names Except the image name that was removed

    updateMemberData($user_info['id'], array('wizard' => $pop));

        echo '<center><br><h4>The item has been removed</h4></br></center>';

    }
--- End code ---

Navigation

[0] Message Index

[#] Next page

Go to full version