Uutiset:

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

Main Menu
Advertisement:

query issue

Aloittaja The Wizard, maaliskuu 11, 2014, 10:01:31 AP

« edellinen - seuraava »

The Wizard

Hello:

I know the code below works until you hit submit. I also know that the code returns the desired array up until After this point -
$x = implode(',', $c);

So I know my problem is with the update query. Now the question is why wont it work?

thanks

Wiz


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

while ($row = $smcFunc['db_fetch_assoc']($request))
{
if (empty($row))
return false;

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

$context['toy_shop_inv']['items'] = $image;
$smcFunc['db_free_result']($request);

if (isset( $_POST['submit']))
{
//Note a fatal lang error is not needed for "image_number" here
//also you cant use (int)$_POST['image_number'] here - unless you don't what it to work?

$c = array_combine($_POST['image_number'], $_POST['image_name']);
ksort($c);
$x = implode(',', $c);

$smcFunc['db_query']('', '
UPDATE {db_prefix}members
SET wizard = {int:wizard}
WHERE id_member={int:current_member}
LIMIT 1', array(
'current_member' => $user_info['id'],
'wizard' => $x
));
}

All Colours Sam

You're telling the code to terminate the execution right in the middle of a loop:

while ($row = $smcFunc['db_fetch_assoc']($request))
   {
      if (empty($row))
         return false;

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

Instead of returning, use the keyword continue:

while ($row = $smcFunc['db_fetch_assoc']($request))
   {
      if (empty($row))
         continue;

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


And add your check after the while is done:

if (empty($image))
return;


$c = array_combine($_POST['image_number'], $_POST['image_name']);

You're assuming both $_POST values are arrays, array_combine needs two arrays and both of them needs to have the same number of elements otherwise it will return false;
Oh, wouldn't it be great if I *was* crazy? ...then the world would be okay
Suki

The Wizard

Hello Suki:

Still no joy in getting the update query to work. As for the first part of the code it works, and I have made the changes you suggested.

LainaaYou're assuming both $_POST values are arrays, array_combine needs two arrays and both of them needs to have the same number of elements otherwise it will return false;

Both values are arrays, and both values have the same number of elements - tested. I know your going to want to see where this data came from so I included my template function below.


function toy_shop_display_your_toys() 
{
global $txt, $context, $modSettings, $boardurl, $scripturl, $smcFunc;

echo '
<div class="cat_bar">
<h3 class="catbg" align="center">', $txt['toy_shop_display'], '</h3>
</div>
<div class="windowbg">
<span class="topslice"><span></span></span>
<div class="content" style="height:auto;min-height:', $context['toy_shop_box_height'],'px;">

<form action="', $scripturl, '?action=display_your_toys" method="post">
<table align="center" class="wizards_shop_5px_padding">
<tr valign="top">
';

$i = 0;
$bn = 1;

foreach($context['toy_shop_inv']['items'] as $row => $image)
{
$i++;

echo "
<td>
<script language='javascript' type='text/javascript'>
function load".$i."(){
var load = window.open('" . $boardurl . "/Themes/default/toy shop images/big/" . $image . "', ' ', 'scrollbars=no,menubar=no,height=300,width=300,resizable=no,toolbar=no,location=no,status=no');
}
</script>
</td>
";

echo '
<td align="left" valign="top">
<a href="javascript:load'.$i.'()">
<img width="', $modSettings['shopImageWidth'], '" height="', $modSettings['shopImageHeight'], '" src="' . $boardurl . '/Themes/default/toy shop images/small/' . $image . '" align="left" alt="Item Image"/>
</a>
<br /><font color="green">Number :</font><input type="text" name="image_number[]" size="2" value="', $bn, '" />
<input type="hidden" name="image_name[]" value="', $image,'" />
</td>
';

$wiz = 7;

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

$bn++;
}

// Do not remove code <td></td></tr></table> or The W3C validator will be very unhappy!
echo '<td></td></tr></table>';

echo '
<br />
<center>
<input type="submit" value="', $txt['toy_shop_admin_save_changes'], '" />
</center>
</form>
';

// Test system remove code after use
echo '<pre>', print_r($context['test_area_7']), '</pre>';

echo '
</div>
<span class="botslice"><span></span></span>
</div>
';
}


Any ideas on why the update will not work?

Wiz


All Colours Sam

Well, to be honest, I ave no idea what you're trying to do.

Best thing you can do is do a var_dump($x);die;  right before your update query and see if $x is in fact what you want it to be.  Id $x is OK move your check to see if $c is what you want it to be.
Oh, wouldn't it be great if I *was* crazy? ...then the world would be okay
Suki

The Wizard

They say a picture is worth a thousand words -



My goal is to allow the user to change the order of the images displayed by changing the numbers in the number boxes below the image.

if I use the code below and hit submit-


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

while ($row = $smcFunc['db_fetch_assoc']($request))
{
if (empty($row))
continue;

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

if (empty($image))
return;

$context['toy_shop_inv']['items'] = $image;
$smcFunc['db_free_result']($request);

$c = array_combine($_POST['image_number'], $_POST['image_name']);
ksort($c);
$x = implode(',', $c);

$context['test_area_7'] = $x;


I get the following with print_r -
krynoid,doctor01,doctor02,doctor03,doctor04,doctor05,doctor06,doctor07,doctor08,doctor091
which is the correct data and how it is to be saved.

If I change the numbers in the number boxes and hit submit the data in the array is correctly changed.

Wiz

margarett

SET wizard = {int:wizard}
should be string, right?
Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

LainaaOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

The Wizard

Hello:

Problem Solved!

Below is the working code with all the extras. Feel free to point out errors.

Wiz



function ToyShopDisplayYourToys()
{
global $smcFunc, $txt, $user_info, $context, $boardurl, $scripturl, $modSettings;

loadTemplate('ToyShopUser');
loadLanguage('toy shop languages/ToyShop', 'english');
ToyShopLinks_Credit_Center();

$context['linktree'][] = array(
'url' => $scripturl . '?action=display_your_toys',
'name' => $txt['toy_shop_display_page_title']
);

$context['toy_shop_display_Head'] = $txt['toy_shop_display_page_title'];

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

while ($row = $smcFunc['db_fetch_assoc']($request))
{
if (empty($row))
continue;

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

if (empty($image))
return;

$context['toy_shop_inv']['items'] = $image;
$smcFunc['db_free_result']($request);

if (isset($_POST['image_number']))
{

$c = array_combine($_POST['image_number'], $_POST['image_name']);
ksort($c);
$x = implode(',', $c);

$smcFunc['db_query']('', '
UPDATE {db_prefix}members
SET wizard = {string:wizard}
WHERE id_member={int:current_member}
LIMIT 1', array(
'current_member' => $user_info['id'],
'wizard' => $x
));

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

while ($row = $smcFunc['db_fetch_assoc']($request))
{
if (empty($row))
continue;

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

if (empty($image))
return;

$context['toy_shop_inv']['items'] = $image;
$smcFunc['db_free_result']($request);

$context['sub_template'] = 'toy_shop_display_your_toys';
}

$context['toy_shop_links_remove'] = 15;
$context['toy_shop_links_credit_center'] = 50;

$context['sub_template'] = 'toy_shop_display_your_toys';
}

Advertisement: