Customizing SMF > SMF Coding Discussion

database select box problem

<< < (2/3) > >>

Kays:
OK, something quick and dirty to play with. I'll leave up to you to correct any variables and errors.

Add the following to a page name test.php and upload it to the root of your forums. The access it by url. http://forun_url/test.php. There's a select to remove any items and a input box to add one.


--- Code: ---<?php

require_once('SSI.php');

// explode the images into an array so we can work with them.
if (!empty($user_info['wizard']))
$wizard = explode(',', $user_info['wizard']);
else
$wizard = array();

// $_POST['save'] is set by the "submit" button
if (!empty($_POST['save']))
{
// Adding a new image?
if (!empty($_POST['new_wiz']))
$wizard[] = $_POST['new_wiz'];

// Or removing one?
if (!empty($_POST['rem_wiz']))
{
$key = array_search(, $wizard);
unset($wizard[$key]);
}

updateMemberData($user_info['id'], (array('wizard' => implode(',', $wizard)));
// Refresh the page and clear the post data
redirectexit($scripturl . '/test.php');
}

// Not saving, show the form then
echo '
<form action="', $scripturl, '/test.php">';

// An input box to add a new item
echo '
<input type="text" name="new_wiz" />';

// Or a select to remove one
iif (!empty($wizard))
{
echo '
<select name="rem_wiz">';

foreach($wizard as $wiz))
echo 'option value="', $wiz, '">', $wiz. '</option>';

echo '
</select>';
}
echo '
<input type="submit" name="save" value="Submit" />
</form>';

?>
--- End code ---

Tips
If you haven't already done so, download the enhanced php manual in th help file format and refer to it whenever you see a php function you're not familiar with. Basically, RTFM. :P There's a wealth of information in it. Another good reference to use is the SMF function database. Link on the top of this page under "Support"

If you want to see want's happening and what variables are being passed add a print_r() after SSI.php is included.


--- Code: ---echo '<pre>', print_r($_POST), '</pre>';

--- End code ---

The Wizard:
@Kays

Thank you for the demo and advice. I'm going to spend the next few days studying and hopefully understanding the material.

I'm glad there are users on the board who like to help.

Wiz

The Wizard:
Hello:

The code below works sort of. For some reason all the data in the column wizard shows up in the same seclect box across, and not one at a time vertically.

Could someone point out where I'm going wrong? 


--- Code: ---<?php

require_once('SSI.php');

global $smcFunc, $db_prefix, $user_info;

    // 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'],
     ));
 
    // fetch their information

$wizard = $smcFunc['db_fetch_assoc']($result);

    // Values from array 1

        echo'<select name="cities">'; 
    
// For each value of the array assign variable name "city" 
    
    foreach($array1 as $city){ 
        echo'<option value="'.$city.'">'.$city.'</option>'; 
        } 
        echo'</select>';

?>
--- End code ---

Kays:
In the database wizard is a string. Which is what you are seeing.

What you need to do first is to explode that string into an array.

Also, I don't see $array1 defined. That should be $wizard, but as an array not a string

The Wizard:
Using this code -

--- Code: ---<?php

require_once('SSI.php');

global $smcFunc, $db_prefix, $user_info;

    // 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

$wizard = $smcFunc['db_fetch_assoc']($result);

    // Cut the string up into parts
        
$wizard = explode(',', $wizard);

echo '<pre>', print_r($wizard), '</pre>';
?>
--- End code ---

I get the following error -


--- Quote ---Warning: explode() expects parameter 2 to be string, array given in /test2.php on line 24

1

--- End quote ---

If in the database wizard is a string then why everytime I try and to explode the string I get that error?

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version