4 Replies - 5111 Views - Last Post: 20 October 2010 - 04:04 AM Rate Topic: -----

#1 jmarian1   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 20-October 09

help if textbox not empty get the label and check if exist in database

Posted 01 October 2010 - 06:09 PM

HI, please help me get the right code for my logic below in php. I am a beginner in PHP and I can't get the right code to make it work. I did some coding but not sure if I am in the right way. Please help me. I am about to pull my hair. I am really desperate to make this work. I hope you can help me please.

Basically, the logic are:

I have 18 textbox a-r. I want to check if those textbox are not empty using for (i=3; i< 18; i++)
if not empty, I want to get the label of the textbox and check in the database if exist (comparing label = desnum)
if it exist in the database (database include 3 column: desnum, agencyname, Impact_ID) I want to get the Impact_ID and write it to the 5 hidden textbox if exist with unique Impact_ID only and cannot be duplicated
In the database, my unique is desnum, agencyname that is associated to one of the Impact_ID such as (Homeless, Childhood, ER, Finance, Crime)
If one of the Impact_ID exist in the hidden textbox, move to the other textbox until it has one that doesn't exist in the hidden textbox.
Here is my php code I created:

PHP Code:
$j = 0; 
        $homeless = 0; 
        $childhood = 0; 
        $emergency = 0; 
        $crime = 0; 
        $financial = 0; 
        for ($i=3; $i<18; $i++) 
        { 
            if ($donation[$i] != "") 
            { 
                for ($k=0; $k<sizeof($designation); $k++) 
                { 
                                    foreach ($desnum as $impact => $issue) 
                                    { 
                                     if ($issue == "Homelessness") 
                                    { 
                                        if (empty($clean['impact1'])) 
                                        { 
                                            if ($homeless != 1) 
                                            echo $_POST['impact1'] = "Homelessness"; 
                                            $homeless = 1; 
                                        } 
                                    } 
                                         
                                    else if ($desnum == "Early Childhood Development") 
                                    { 
                                        if (empty($clean['impact2'])) 
                                        { 
                                            if ($childhood != 1) 
                                            { 
                                            echo $_POST['impact2'] = "Early Childhood Development"; 
                                            $childhood = 1; 
                                            } 
                                        } 
                                    } 
                                    else if ($impact == "Financial Stability and Independence") 
                                    { 
                                        if (empty($clean['impact3'])) 
                                        { 
                                            if ($financial != 1) 
                                            { 
                                                echo $_POST['impact3'] = "Financial Stability and Independence"; 
                                                   $financial = 1; 
                                            } 
                                        } 
                                    } 
                                    else if ($impact = "Emergency Crisis and Services") 
                                    { 
                                        if (empty($clean['impact4'])) 
                                        { 
                                            if ($emergency != 1) 
                                            { 
                                            echo $_POST['impact4'] = "Emergency Crisis and Services";                         
                                            $emergency = 1; 
                                             } 
                                        } 
                                    } 
                                    else if ($impact == "Crime and Drug") 
                                    { 
                                        if (empty($clean['impact5'])) 
                                        { 
                                            if ($crime != 1) 
                                            { 
                                            echo $_POST['impact5'] =  "Crime and Drug"; 
                                            $crime = 1; 
                                        } 
                                    } 
                                    } 
                                     
                     
                } 
                 
                 
            } 
         
        }  

and this is my html table:

PHP Code:
<table> 
                <tr> 
                    <td style="width:380px;text-align:left"><label for="d"  id="D" name="D" >Crime <a href="javascript:alert('Ensuring ');">what is this?</a></label></td> 
         
        <td style="float:right">  $<input id="d" name="d" class="text" type="text" value="<?php safeEcho($form['d'])?>" style="width:90px;" onchange="CalculateTotal();" /> 
     <?php helper_error('d');?> </td> <td> <input name="impact1" type="hidden"  />    
</td> 
</tr> 
                <tr> 
                    <td style="width:380px;text-align:left"><label for="e"  id="E" name="E">Childhood <a href="javascript:alert('Children');">what is this?</a></label></td> 
         
        <td style="float:right">  $<input id="e" name="e" class="text" type="text" value="<?php safeEcho($form['e'])?>" style="width:90px;" onchange="CalculateTotal()" /> 
     <?php helper_error('e');?> </td> <td> <input name="impact2" type="hidden" />    
</td> 

                </tr> 
                <tr> 
                    <td style="width:380px;text-align:left"><label for="f"  id="F" name="F">Emergency <a href="javascript:alert('Helping families ');">what is this?</a></label></td> 
         
        <td style="float:right">  $<input id="f" name="f" class="text" type="text" value="<?php safeEcho($form['f'])?>" style="width:90px;" onchange="CalculateTotal()" /> 
     <?php helper_error('f');?> </td><td> <input name="impact3" type="hidden" />    

                </tr> 
                <tr> 
                    <td style="width:380px;text-align:left"><label for="g"  id="G" name="G">Financial<a href="javascript:alert('Providing programs');">what is this?</a></label></td> 
         
        <td style="float:right">  $<input id="g" name="g" class="text" type="text" value="<?php safeEcho($form['g'])?>" style="width:90px;" onchange="CalculateTotal()"/> 
     <?php helper_error('g');?> </td><td> <input name="impact4" type="hidden" />    

                </tr> 
                <tr> 
                    <td style="width:380px;text-align:left"><label for="h"  id="H" name="H">Homelessness <a href="javascript:alert('Preventing families ');">what is this?</a></label></td> 
         
        <td style="float:right">  $<input id="h" name="h" class="text" type="text" value="<?php safeEcho($form['h'])?>" style="width:90px;" onchange="CalculateTotal()"/> 
     <?php helper_error('h');?> </td><td> <input name="impact5" type="hidden" />    

                </tr> 
                
                
</table>


Is This A Good Question/Topic? 0
  • +

Replies To: help if textbox not empty get the label and check if exist in database

#2 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3813
  • View blog
  • Posts: 13,857
  • Joined: 08-August 08

Re: help if textbox not empty get the label and check if exist in database

Posted 01 October 2010 - 07:00 PM

  • $donation is not set, so donation[$i] will be equal to "" and your loops will not run.
  • Using tables for formatting is wrong.

Was This Post Helpful? 0
  • +
  • -

#3 Guest_Jenny*


Reputation:

Re: help if textbox not empty get the label and check if exist in database

Posted 15 October 2010 - 02:17 PM

Hi. Thanks for your help. I am not that good in php and just trying to learn by myself. Would it be possible to give me idea on how to fix my code? Thanks.
Was This Post Helpful? 0

#4 Guest_jmarian1*


Reputation:

Re: help if textbox not empty get the label and check if exist in database

Posted 19 October 2010 - 08:17 PM

Hi, please help. I revise my code and although it sounds so simple, I can't get the right code.
below is my sql connect:
require_once ('connectionfile');
$query = "SELECT * FROM tablename WHERE columnname = '$_POST[textboxes]'";
$result = mysql_query($query) or die (mysql_error());
$n = mysql_num_rows($result);
if ($n > 0)
{
  $row=mysqli_fetch_assoc($result);
 } 


Somehow I can't pass the Impact_ID column in my database to all the $impact1-5. What am I doing wrong? Please help.
<?php while ($row = mysql_fetch_array($result))
{
	if ($amountlabels != "")
	{
 	if ($impact = $row['desnum'])
 	{
	$issue = $row['Impact_ID'];
   if (($issue = "Homelessness") && ($impact1 = ""))
    {
	   $impact1 = $issue;
	   echo $impact1;
	 }
   else if (($issue = "Early Childhood Development") && ($impact2 = ""))
    {
	   $impact2 = $issue;
	   echo $impact2;
	 } 
	else if (($issue = "Financial Stability and Independence") && ($impact3 = ""))
    {
	   $impact3 = $issue;
	   echo $impact3;
	 } 
	else if (($issue = "Emergency and Crisis Services") && ($impact4 = ""))
    {
	   $impact4 = $issue;
	   echo $impact4;
	 }
	 else if (($issue = "Crime and Drug Use") && ($impact5 = ""))
    {
	   $impact5 = $issue;
	   echo $impact5;
	 } 
  } 
}

}

Below is my table to help the code:
<label for="i">Label1</label>
    <input id="designation_list1" name="designation_list[]" class="text" type="text" value="<?php 

safeEcho($row['designation_list[]'])?>" style="width:90px;" />
    &nbsp;
<label for="i">Amount: $</label>
    <input id="i" name="i" class="text" type="text" value="<?php safeEcho($form['i'])?>" 

style="width:90px;" onchange="CalculateTotal(this.value,99);"/>
<?php helper_error('designation_list1');?>
    <?php helper_error('i');?>
</div>
<div>
  <label for="j">Label2: </label>
  <input id="designation_list2" name="designation_list[]" class="text" type="text" value="<?php 

safeEcho($row['designation_list[]'])?>" style="width:90px;"  />&nbsp;&nbsp;
    <label for="j">Amount: $</label>
  <input id="j" name="j" class="text" type="text" value="<?php safeEcho($form['j'])?>" 

style="width:90px;" onchange="CalculateTotal(this.value,99);" />
    <?php helper_error('designation_list[]');?>
    <?php helper_error('j');?>
</div>

Was This Post Helpful? 0

#5 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6248
  • View blog
  • Posts: 24,016
  • Joined: 23-August 08

Re: help if textbox not empty get the label and check if exist in database

Posted 20 October 2010 - 04:04 AM

($issue = "Homelessness")

That's an ASSIGNMENT, not a COMPARISON. = for assignment, == for comparison.

EDIT: Another suggestion. What exactly are you doing here?
require_once ('connectionfile');
$query = "SELECT * FROM tablename WHERE columnname = '$_POST[textboxes]'";
$result = mysql_query($query) or die (mysql_error());
$n = mysql_num_rows($result);
if ($n > 0)
{
  $row=mysqli_fetch_assoc($result);
 } 


you're running a select on everything, fetching only the first row and then apparently throwing it away. There's likely a better way to accomplish whatever it is you're after here.

A couple of things I would change in that code for future reference.
// Your connection file should have a .php extension. Otherwise, there is the possibility a malicious user could download the file and get your DB connection info. If it has a PHP extension, the web server will not display the data to the end user in that case.
require_once ('connectionfile.php');

// Accessing associative arrays through string-based keys should always use STRINGS, so [il]$_POST[textboxes][/il] should be [il]$_POST['textboxes'][/il].
$query = "SELECT * FROM tablename WHERE columnname = '{$_POST['textboxes']}'";

// When building a query from data provided, it is easier to debug when you print the query when it fails. In a production environment this should go to an error log, rather than using [il]die()[/il] so you don't expose your database details to potentially malicious end users.
$result = mysql_query($query) or die ("Query $query failed: " . mysql_error());
$n = mysql_num_rows($result);
if ($n > 0)
{
  $row=mysqli_fetch_assoc($result);
 } 


This post has been edited by JackOfAllTrades: 20 October 2010 - 05:00 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1