Page 1 of 1

PHP/MySQL modification code doesnt work, perplexing

Posted: Tue May 18, 2010 11:12 am
by cap2cap10
Greetings again, PHP technorati. Again, I grace your doorstep with another perplexing simple problem. I simply want
the code to not print anything if there is no data in the MySQL field. This code works on one part of the page, but
does not work in the other part. It prints [To]. I have tried remove the [To] by setting it as a variable, but the first part
of the code is ignored?!! Here is the code snipet:

Code: Select all

<?
if ($info_1['job_duty_1'] == "") {
  echo" ";
  }
else {
print "<b>".$info_1['start_mo_1'] ."&nbsp;" .$info_1['start_yr_1'] ."&nbsp; ".$info_1['to']." &nbsp;".$info_1['end_mo_1'] ."&nbsp;".$info_1['end_yr_1'] ."</b><br>
        <b>".$info_1['boss_1']."<BR>" .$info_1['job_title_1']."</b><BR>
    <b>".$info_1['job_duty_1']. "</b><p>";
}
if ($info_1['job_duty_2'] == "") {
  echo" ";
  }
else {
print "<b>".$info_1['start_mo_2 '] ."&nbsp;" .$info_1['start_yr_2'] ."&nbsp; ".$info_1['to']." &nbsp;".$info_1['end_mo_2'] ."&nbsp;".$info_1['end_yr_2'] ."</b><br>
        <b>".$info_1['boss_2']."<BR>" .$info_1['job_title_2']."</b><BR>
        <b>".$info_1['job_duty_2']. "</b><p>";

        }
if ($info_1['job_duty_3'] == "") {
  echo" ";
  }
else {
print "<b>".$info_1['start_mo_3'] ."&nbsp;" .$info_1['start_yr_3'] ."&nbsp; ".$info_1['to']." &nbsp;".$info_1['end_mo_3'] ."&nbsp;".$info_1['end_yr_3'] ."</b><br>
        <b>".$info_1['boss_3']."<BR>" .$info_1['job_title_3']."</b><BR>
        <b>".$info_1['job_duty_3']. "</b><p>";
}
if ($info_1['job_duty_4'] == "") {
  echo" ";
  }
else {
print "<b>".$info_1['start_mo_4'] ."&nbsp;" .$info_1['start_yr_4'] ."&nbsp; ".$info_1['to']." &nbsp;".$info_1['end_mo_4'] ."&nbsp;".$info_1['end_yr_4'] ."</b><br>
        <b>".$info_1['boss_4']."<BR>" .$info_1['job_title_4']."</b><BR>
        <b>".$info_1['job_duty_4']."</b><p>";
}
if ($info_1['job_duty_5'] == "") {
print" ";
  }
else {
print "<b>".$info_1['start_mo_5'] ."&nbsp;" .$info_1['start_yr_5'] ."&nbsp; ".$info_1['to']." &nbsp;".$info_1['end_mo_5'] ."&nbsp;".$info_1['end_yr_5 '] ."</b><br>
        <b>".$info_1['boss_5']."<BR>" .$info_1['job_title_5']."</b><BR>
        <b>".$info_1['job_duty_5']."</b><p>";
} 
if ($info_1['job_duty_6'] == "") {
print" ";
  }
else {
print "<b>".$info_1['start_mo_6'] ."&nbsp;" .$info_1['start_yr_6'] ."&nbsp; ".$info_1['to']." &nbsp;".$info_1['end_mo_6'] ."&nbsp;".$info_1['end_yr_6 '] ."</b><br>
        <b>".$info_1['boss_6']."<BR>" .$info_1['job_title_6']."</b><BR>
        <b>".$info_1['job_duty_6']."</b><p>";
}
if ($info_1['job_duty_7'] == "") {
print" ";
  }
else {
print "<b>".$info_1['start_mo_7'] ."&nbsp;" .$info_1['start_yr_7'] ."&nbsp; ".$info_1['to']." &nbsp;".$info_1['end_mo_7'] ."&nbsp;".$info_1['end_yr_7 '] ."</b><br>
        <b>".$info_1['boss_7']."<BR>" .$info_1['job_title_7']."</b><BR>
        <b>".$info_1['job_duty_7']."</b><p>";
}
if ($info_1['job_duty_8'] == "") {
print" ";
  }
else {
print "<b>".$info_1['start_mo_8'] ."&nbsp;" .$info_1['start_yr_8'] ."&nbsp; ".$info_1['to']." &nbsp;".$info_1['end_mo_8'] ."&nbsp;".$info_1['end_yr_8 '] ."</b><br>
        <b>".$info_1['boss_8']."<BR>" .$info_1['job_title_8']."</b><BR>
        <b>".$info_1['job_duty_8']."</b><p>";
}
if ($info_1['job_duty_9'] == "") {
print" ";
  }
else {
print "<b>".$info_1['start_mo_9'] ."&nbsp;" .$info_1['start_yr_9'] ."&nbsp; ".$info_1['to']." &nbsp;".$info_1['end_mo_9'] ."&nbsp;".$info_1['end_yr_9 '] ."</b><br>
        <b>".$info_1['boss_9']."<BR>" .$info_1['job_title_9']."</b><BR>
        <b>".$info_1['job_duty_9']."</b><p>";
}
if ($info_1['job_duty_10'] == "") {
print" ";
  }
else {
print "<b>".$info_1['start_mo_10'] ."&nbsp;" .$info_1['start_yr_10'] ."&nbsp; ".$info_1['to']." &nbsp;".$info_1['end_mo_10'] ."&nbsp;".$info_1['end_yr_10 '] ."</b><br>
        <b>".$info_1['boss_10']."<BR>" .$info_1['job_title_10']."</b><BR>
        <b>".$info_1['job_duty_10']."</b><p>";
}

?>
Problem: it keeps printing the [To] even if the job_duty field is empty??!! :banghead:
A similiar code works on the another part of the page. Please enlighten me as soon as
possible! Frustration factor is starting to increase.

Thanks in advance,


Batoe

Re: PHP/MySQL modification code doesnt work, perplexing

Posted: Tue May 18, 2010 11:35 am
by flying_circus
Can we see the rest of your code? I'd like to know where and how $info_1 is created.

Re: PHP/MySQL modification code doesnt work, perplexing

Posted: Tue May 18, 2010 11:49 am
by cap2cap10
OK, here is more of the code:

Code: Select all

require_once 'db_1.php';
$resume = mysql_query("SELECT * FROM resume WHERE `portID` = '".$_GET['portID']."'")
or die(mysql_error());
$info_1 = mysql_fetch_array( $resume );

$account = mysql_query("SELECT * FROM account WHERE `portID` = '".$info_1['portID']."'")
or die(mysql_error());
$info_2 = mysql_fetch_array( $account );

$profile = mysql_query("SELECT * FROM profile WHERE `portID` = '".$info_1['portID']."'")
or die(mysql_error());
$info_3 = mysql_fetch_array( $profile );

$login = mysql_query("SELECT * FROM login WHERE `portID` = '".$info_1['portID']."'")
or die(mysql_error());
$info_4 = mysql_fetch_array( $login );

this part of the code works, there is no problem accessing the database and retrieving information. Here is another part of the code that
works perfectly!

Code: Select all

<? if ($info_3['lang_1'] == "") {
  echo" ";
  }
else {
print "1-<b>".$info_3['lang_1'] . "</b>-<b>".$info_3['fluent_1'] . "</b><p>";
}
       if ($info_3['lang_2'] == "") {
  echo" ";
  }
else {
print "2-<b>".$info_3['lang_2'] . "</b>-<b>".$info_3['fluent_2'] . "</b><p>";
}
       if ($info_3['lang_3'] == "") {
  echo" ";
  }
else {
print "3-<b>".$info_3['lang_3'] . "</b>-<b>".$info_3['fluent_3'] . "</b><p>";
}
       if ($info_3['lang_4'] == "") {
  echo" ";
  }
else {
print "4-<b>".$info_3['lang_4'] . "</b>-<b>".$info_3['fluent_4'] . "</b><p>";
}
       if ($info_3['lang_5'] == "") {
  echo" ";
  }
else {
print "5-<b>".$info_3['lang_5'] . "</b>-<b>".$info_3['fluent_5'] . "</b><p>";
}       ?>
I am still trying to find an answer.

Thanks again,

Batoe

Re: PHP/MySQL modification code doesnt work, perplexing

Posted: Tue May 18, 2010 12:18 pm
by flying_circus
Without being able to see your database structure, I'm kind of stabbing in the dark, but I did notice that every other field has a suffix, except the to field.

should $info_1['to'] be $info_1['to_yr_x']?

Re: PHP/MySQL modification code doesnt work, perplexing

Posted: Tue May 18, 2010 2:14 pm
by mikosiko
cap2cap10 wrote:....

Problem: it keeps printing the [To] even if the job_duty field is empty??!!
What exactly do you mean by "keeps printing the [To]" ?... you mean is printing "[To]" literally ?... also you said... "even if the job_duty field is empty" ... but you have 10 job_duty field... to which one are you referring to?... or all of them?

and if you allow me... let me give you a little contribution to improve your code.... you can replace all those if's with something like this:

Code: Select all

for ($i = 1; $i<=10; $i++) {
    if (!is_null($info_1["job_duty_$i"])) {
	   print "<b>" . $info_1["start_mo_$i"] 
	               . "&nbsp;" 
		       . $info_1["start_yr_$i"] 
		       . "&nbsp;"
		       . $info_1["to"]
		       . "&nbsp;"
		       . $info_1["end_mo_$i"] 
		       . "&nbsp;"
		       . $info_1["end_yr_$i"] . "</b><br><b>"
	               . $info_1["boss_$i"] . "<br>" 
		       . $info_1["job_title_$i"] . "</b><br><b>"
		       . $info_1["job_duty_$i"]. "</b><p>";
    }
}

Re: PHP/MySQL modification code doesnt work, perplexing

Posted: Tue May 18, 2010 3:16 pm
by cap2cap10
Thanks for the code, but it does exactly the same thing. Which leaves me right where I started :banghead: . The problem is still that
the code ignores print"nothing" in the absence of a value for job_duty. Regardless of which job_duty variable we are referring to. To
is simply a string constant that does not do anything except be itself, a preposition.

thanks again,


Batoe

"The road ahead is filled with obstacles, but also success!" The unknown Optimist

Re: PHP/MySQL modification code doesnt work, perplexing

Posted: Tue May 18, 2010 3:32 pm
by mikosiko
sorry... but definitely I don't understand what you are saying having in mind the code that you provided... that code or the simplified one that I did provided to you doesn't print anything when your variable job_duty_$i is null... or maybe what you want to validate is "if any job_duty is null then don't print anything, else for each job_duty not null print the associated values" ?

is that what you want?... if so... modify the provided code is fairly easy and for sure you can figure it out

Miko

Re: PHP/MySQL modification code doesnt work, perplexing

Posted: Tue May 18, 2010 3:51 pm
by cap2cap10
Well, what I want is for the code to check the database to see if each field ,job_duty, has data. If the job_duty field is occupied with data, then print
all associated variables in the afore mentioned code. If the job_duty field is empty, do not print the associated variables and my "To" preposition.
Unfortunately, even if job_duty fields are empty it trys to print empty fields including my "To" preposition. this results in an awkward looking series of
To(s) in one column.
TO
TO
TO
TO
TO.....
This annoys me and it is not acceptable.

I hope I was able to clear up any vagueness on my part.
Thanks again,

Batoe

Re: PHP/MySQL modification code doesnt work, perplexing

Posted: Tue May 18, 2010 4:12 pm
by mikosiko
the provided code does exactly that.... only thing that I don't see in any place in your code where are you printing your "To" preposition... is not there... what you have in your code is $info_1["to"] and neither that is printed.

maybe you didn't post all the code that you are dealing with... specially the part where you are printing your "To" preposition.

clarify and we will try to help