Setting a "where" value from a form selection

11 posts by 2 authors in: Forums > CMS Builder
Last Post: July 5, 2012   (RSS)

By gkornbluth - July 2, 2012

Hi All,

OK, I’m lost again.

I’m trying to set a “Where” value from a form and I’m having a bit of trouble.

This hard coded code works correctly to limit the records shown to those matching the where statement.
list($client_uploadsRecords, $client_uploadsMetaData) = getRecords(array(
'tableName' => 'client_uploads',
'where' => ' project_title = "Test Event 1" ',
));


However this doesn’t and I can’t figure out why.
$where = "";
if (@$FORM['where'] == 'a') { $where = 'Test Event 1'; }
if (@$FORM['where'] == 'b') { $where = 'Test Event 2'; }

list($client_uploadsRecords, $client_uploadsMetaData) = getRecords(array(
'tableName' => 'client_uploads',
'where' => ' project_title = "$where" ',
));

The form in the body is:
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<select name="where">
<option value="a">Test Event 1</option>
<option value="b">Test Event 2</option>
</select>

<input type="submit" name="submit" value="Choose an exhibition to View">
</form>


BTW, an <?php echo $where ?> at the top of the body changes correctly with the form selection.

Any suggestions appreciated.

Thanks,

Jerry Kornbluth
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Re: [Jason] Setting a "where" value from a form selection

By gkornbluth - July 2, 2012

Hi Jason,

Your suggestion worked and I appreciate the education.

Jerry
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Re: [gkornbluth] Setting a "where" value from a form selection

By gkornbluth - July 3, 2012

Hi Jason,

Well I’ve upped the ante and I’m stuck again...

This time I’m trying to populate the options in the pull down list from the values in a master list table.

The options in the list work OK but I’m not sure how to get them to populate the $where, when using what I think is a modified version of the original substitution idea that was suggested for security.

$where = "";
if (@$FORM['where'] == 'a') { $where = 'Test Event 1'; }
if (@$FORM['where'] == 'b') { $where = 'Test Event 2'; }

list($client_uploadsRecords, $client_uploadsMetaData) = getRecords(array(
'tableName' => 'client_uploads',
'where' => " project_title = '$where'",
));

I’m trying to use the record number as the substitution value, and the record title as the actual $where value, and I’m sure that the code I guessed at is not right at all.

At the top of the page, I added :
list($master_exhibition_listRecords, $master_exhibition_listMetaData) = getRecords(array(
'tableName' => 'master_exhibition_list',
));

$names = array();
foreach ($master_exhibition_listRecords as $record){
$names[$record['title']]=$record['title'];
$numbers[$record['num']]=$record['num'];
}
?>
<?php
$where = "";
?>
// This is the code that’s I guessed at and which doesn’t work.

<?php foreach ($master_exhibition_listRecords as $record): ?>

<?php if (@$FORM['where'] == $record['num'] { $where = $record['title']}); ?>

<?php endforeach; ?>

//end non-working code

<?php
list($client_uploadsRecords, $client_uploadsMetaData) = getRecords(array(
'tableName' => 'client_uploads',
'where' => " project_title = '$where'",

));

?>

And in the form:
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>">

<select type="text" name="where">
<option value=""><span class="body-text-bold">Select</span></option>

<?php foreach($names as $name): ?>
<option value="<?php echo $umbers;?>"><?php echo $name;?></option>
<?php endforeach?>
</select>

<input type="submit" name="submit" value="Select An Exhibition And Click To View">
</form>

Thanks,

Jerry Kornbluth
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Re: [gkornbluth] Setting a "where" value from a form selection

By Jason - July 4, 2012

Hi Jerry,

When you use debugSql on your getRecords call, what do you see in your where clause?

Are you getting

WHERE project_title = '' ?

From looking at your code, it seems like the problem is in your select options:

Looking at the code here:

<?php foreach($names as $name): ?>
<option value="<?php echo $umbers;?>"><?php echo $name;?></option>
<?php endforeach?>
</select>


I'm not sure where the array $names is coming from. The problem, however, is in the value attribute where we are outputting $umbers (not sure if this is supposed to be $numbers?).

This variable is not referenced anywhere else in the code you posted. Also, it doesn't seem to be part of the foreach loop. My guess is that this is an undefined variable. This mean that in the foreach loop where you attempt to set your $where variable, your if statement is never successful, so $where is always empty.

Hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Setting a "where" value from a form selection

By gkornbluth - July 4, 2012 - edited: July 4, 2012

Hi Jason,

You were right. The $umbers should have been $numbers (gotta stop working close to midnight) but that didn't fix the problem

Sorry that I didn't post the entire page before (it's attached now), it would have answered your other questions.

You can see the results of the debugSql if you pull up the page at:

http://artistsofpalmbeachcounty.org/submissions/submissions-spreadd.php

There's a viewer with Test Event 1 (the only event with any records) hard coded in the where so you can see the results as they should be, at:

http://artistsofpalmbeachcounty.org/submissions/submissions-spreade.php

Thanks for helping with this.

Jerry
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
Attachments:

submissions-spreadd.php 12K

Re: [gkornbluth] Setting a "where" value from a form selection

By Jason - July 4, 2012

Hi Jerry,

The issue is that $numbers is an array, so using echo $numbers, is not going to give you the results you expect. When you created the $numbers and $names array, there is nothing that connects them.

Try this:

$numToName = array();
foreach ($master_exhibition_listRecords as $record){
$numToName[$record['num']] = $record['title'];
}


Then output like this:

<select name="where">
<option value=""><span class="body-text-bold">Select</span></option>

<?php foreach($numToName as $num => $name): ?>
<option value="<?php echo $num;?>"><?php echo $name;?></option>
<?php endforeach?>
</select>


Hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Setting a "where" value from a form selection

By gkornbluth - July 4, 2012 - edited: July 4, 2012

Thank you Jason,

It's a bit late here but I'll give it a go first thing tomorrow.

You're the best...

Jerry
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Re: [Jason] Setting a "where" value from a form selection

By gkornbluth - July 5, 2012

Hi jason,

I probably missed something simple in your suggestion, but I’m still having a problem getting this to work.

In http://artistsofpalmbeachcounty.org/submissions/submissions-spreadg.php (attached), I replaced this code:

$names = array();
foreach ($master_exhibition_listRecords as $record){
$names[$record['title']]=$record['title'];
$numbers[$record['num']]=$record['num'];

}


With your suggested code:
$numToName = array();
foreach ($master_exhibition_listRecords as $record){
$numToName[$record['num']] = $record['title'];
}


And in the form I replaced this:
<select type="text" name="where">
<option value=""><span class="body-text-bold">Select</span></option>

<?php foreach($names as $name): ?>
<option value="<?php echo $numbers;?>"><?php echo $name;?></option>
<?php endforeach?>
</select>



With your suggested “select” code:
<select name="where">
<option value=""><span class="body-text-bold">Select</span></option>

<?php foreach($numToName as $num => $name): ?>
<option value="<?php echo $num;?>"><?php echo $name;?></option>
<?php endforeach?>
</select>
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
Attachments:

submissions-spreadg.php 12K

Re: [gkornbluth] Setting a "where" value from a form selection

By Jason - July 5, 2012

Hi Jerry,

The code in your select box is now working as expected. The issue, it turns out, was in the structure of your if statement.

If you look here:

<?php if (@$FORM['where'] == $record['num'] { $where = $record['title']});?>

because of the placement of the closing parenthesis, the assignment code is actually part of the if expression. This means it is executed every time, which is why its always the last item in the list being put into the WHERE clause.

Try this:

<?php if (@$FORM['where'] == $record['num']) { $where = $record['title'];}?>

That should take care of the issue for you.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/