if something exists display X

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

blade_922
Forum Contributor
Posts: 132
Joined: Wed Jul 12, 2006 4:57 pm

if something exists display X

Post by blade_922 »

Hey i know this is pretty simple but cant get my head around it.

What i have is a table with many fields. 3 Fields are field_1 field_2 and field_3 all are blog type. And through my site i type a short sentence in to each of these fields at different times.

I want to execute a query whereby IF there is anything inserted into Field_1 then display "X" same with Field_2 with XX and same with Field_ with XXX.

So

If there is something in Field_1 display X, if there is something in Field 1 and Field 2 then display XX and if there is something in Field_1 Field_2 and Field_3 then display XXX.

(X is just a letter and doesnt stand for anything)

I've been trying to get my head round it but cant seem to. Here is what i have so far

Code: Select all

$checkstrike=mysql_query("SELECT * FROM ladder_$ladder[id]");
$checkstrike=mysql_fetch_array($checkstrike);

if($Field_1){ echo 'X';


}else{
$Field_1="";
}




<td width='10%' valign='center' align='center'><b><font face='verdana' size='-2'>$Field_1</font></b></td>

At this point i managed to display the contents of Field_1 in the html above, but i dont want the content, i want to display X as i mentioned above.

Any idea's.
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

standard if else

Post by yacahuma »

Code: Select all

if (strlen(trim($field1))>0  && strlen(trim($field2))>0 && strlen(trim($field3))>0
  echo 'XXX';
else if (strlen(trim($field1))>0  && strlen(trim($field2))>0 )
  echo 'XX';
else if (strlen(trim($field1))>0 )
   echo 'X';
User avatar
iknownothing
Forum Contributor
Posts: 337
Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia

Post by iknownothing »

That code looks like it missing heaps, but if you say it works...

This should do the trick...

Code: Select all

if($Field_1){ 
$Field_1 =  'X'; 
}
else{
$Field_1 =  '';
}
User avatar
robshanks
Forum Commoner
Posts: 32
Joined: Sun Aug 05, 2007 9:27 pm
Location: Hull, UK

Post by robshanks »

Or:
This will only check field_2 if field_1 isn't empty, and field_3 if field_1 and field_2 aren't empty

Code: Select all

<?php
$checkstrike=mysql_query("SELECT * FROM ladder_".$ladder[id]);
$checkstrike=mysql_fetch_array($checkstrike);
$output="";
if(strlen(trim($checkstrike['field_1']>0)))
{
  $output="X";
  if(strlen(trim($checkstrike['field_2']>0)))
  {
    $output="XX";
    if(strlen(trim($checkstrike['field_3']>0)))
    {
      $output="XXX";
    }
  }
}
echo "<td width='10%' valign='center' align='center'><b><font face='verdana' size='-2'>".$output."</font></b></td>""
?>
Alternatively:
This will show X for fields with data in them (e.g. fields 1 and 3 empty 2 has data would display -X-):

Code: Select all

<?php
$checkstrike=mysql_query("SELECT * FROM ladder_".$ladder[id]);
$checkstrike=mysql_fetch_array($checkstrike);
$output="---";
if(strlen(trim($checkstrike['field_1']>0)))
{
  $output[0]="X";
}
if(strlen(trim($checkstrike['field_2']>0)))
{
  $output[1]="X";
}
if(strlen(trim($checkstrike['field_3']>0)))
{
  $output[2]="X";
}
echo "<td width='10%' valign='center' align='center'><b><font face='verdana' size='-2'>".$output."</font></b></td>""
?>
blade_922
Forum Contributor
Posts: 132
Joined: Wed Jul 12, 2006 4:57 pm

Post by blade_922 »

robshanks wrote:Or:
This will only check field_2 if field_1 isn't empty, and field_3 if field_1 and field_2 aren't empty

Code: Select all

<?php
$checkstrike=mysql_query("SELECT * FROM ladder_".$ladder[id]);
$checkstrike=mysql_fetch_array($checkstrike);
$output="";
if(strlen(trim($checkstrike['field_1']>0)))
{
  $output="X";
  if(strlen(trim($checkstrike['field_2']>0)))
  {
    $output="XX";
    if(strlen(trim($checkstrike['field_3']>0)))
    {
      $output="XXX";
    }
  }
}
echo "<td width='10%' valign='center' align='center'><b><font face='verdana' size='-2'>".$output."</font></b></td>""
?>
Alternatively:
This will show X for fields with data in them (e.g. fields 1 and 3 empty 2 has data would display -X-):

Code: Select all

<?php
$checkstrike=mysql_query("SELECT * FROM ladder_".$ladder[id]);
$checkstrike=mysql_fetch_array($checkstrike);
$output="---";
if(strlen(trim($checkstrike['field_1']>0)))
{
  $output[0]="X";
}
if(strlen(trim($checkstrike['field_2']>0)))
{
  $output[1]="X";
}
if(strlen(trim($checkstrike['field_3']>0)))
{
  $output[2]="X";
}
echo "<td width='10%' valign='center' align='center'><b><font face='verdana' size='-2'>".$output."</font></b></td>""
?>


Hey

The second one is the one im looking for(i think lol). I have tried it and it just keeps showing up with "---" even if there is something in field_1. So when an X should be coming up it still comes up ---

Any idea whats causing this?
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

His if statements are wrong.

Code: Select all

if(strlen(trim($checkstrike['field_x']>0))) 
should be

Code: Select all

if(strlen(trim($checkstrike['field_x']))>0) 
User avatar
robshanks
Forum Commoner
Posts: 32
Joined: Sun Aug 05, 2007 9:27 pm
Location: Hull, UK

Post by robshanks »

Try:

Code: Select all

<?php
$checkstrike=mysql_query("SELECT * FROM ladder_".$ladder[id]);
$checkstrike=mysql_fetch_array($checkstrike);
$output=str_split("---");
if(strlen(trim($checkstrike['field_1']))>0)
{
  $output[0]="X";
}
if(strlen(trim($checkstrike['field_2']))>0)
{
  $output[1]="X";
}
if(strlen(trim($checkstrike['field_3']))>0)
{
  $output[2]="X";
}
echo "<td width='10%' valign='center' align='center'><b><font face='verdana' size='-2'>".$output."</font></b></td>";
?>
Thanks Zoxive I got the >0 in the wrong place.

Also str_split() forces an array.
blade_922
Forum Contributor
Posts: 132
Joined: Wed Jul 12, 2006 4:57 pm

Post by blade_922 »

robshanks wrote:Try:

Code: Select all

<?php
$checkstrike=mysql_query("SELECT * FROM ladder_".$ladder[id]);
$checkstrike=mysql_fetch_array($checkstrike);
$output=str_split("---");
if(strlen(trim($checkstrike['field_1']))>0)
{
  $output[0]="X";
}
if(strlen(trim($checkstrike['field_2']))>0)
{
  $output[1]="X";
}
if(strlen(trim($checkstrike['field_3']))>0)
{
  $output[2]="X";
}
echo "<td width='10%' valign='center' align='center'><b><font face='verdana' size='-2'>".$output."</font></b></td>";
?>
Thanks Zoxive I got the >0 in the wrong place.

Also str_split() forces an array.


Hey thanks for your help so far.

Im getting this error:

Fatal error: Call to undefined function: str_split() in /home/gtcave/public_html/blogpsp/ladders/standings.php

any idea whats causing this?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

str_split() is php5 only, however you can replace that line with

Code: Select all

$output = array();
and this line

Code: Select all

echo "<td width='10%' valign='center' align='center'><b><font face='verdana' size='-2'>".$output."</font></b></td>";
will need to be replaced with

Code: Select all

echo "<td width='10%' valign='center' align='center'><b><font face='verdana' size='-2'>". implode('', $output)."</font></b></td>";
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

str_split() is a php5 function, most likely you don't have php5.
blade_922
Forum Contributor
Posts: 132
Joined: Wed Jul 12, 2006 4:57 pm

Post by blade_922 »

Cheers JCart.

I've altered the code and now have this

Code: Select all

$checkstrike=mysql_query("SELECT * FROM ladder_$ladder[id]"); 
$checkstrike=mysql_fetch_array($checkstrike); 
$output=array();
if(strlen(trim($checkstrike['strike_1']))>0) 
{ 
  $output[0]="X"; 
} 
if(strlen(trim($checkstrike['strike_2']))>0) 
{ 
  $output[1]="XX"; 
} 
if(strlen(trim($checkstrike['strike_3']))>0) 
{ 
  $output[2]="XXX"; 
}
and this is displayed somewhere down the page

Code: Select all

<td width='10%' valign='center' align='center'><b><font face='verdana' size='-2'>". implode('', $output)."</font></b></td>
okay so now there is no error on the page but there is no X appearing where it should be. Any ideas?

Thanks for all your help so far, i dont know what i would do without ya! :)
User avatar
robshanks
Forum Commoner
Posts: 32
Joined: Sun Aug 05, 2007 9:27 pm
Location: Hull, UK

Post by robshanks »

You need to put:

Code: Select all

$checkstrike=mysql_query("SELECT * FROM ladder_$ladder[id]");
$checkstrike=mysql_fetch_array($checkstrike);
$output=array("-","-","-");
if(strlen(trim($checkstrike['strike_1']))>0)
{
  $output[0]="X";
}
if(strlen(trim($checkstrike['strike_2']))>0)
{
  $output[1]="X";
}
if(strlen(trim($checkstrike['strike_3']))>0)
{
  $output[2]="X";
}
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Try a bit of debugging to see whats happening.

Code: Select all

$checkstrike=mysql_query("SELECT * FROM ladder_$ladder[id]");
$checkstrike=mysql_fetch_array($checkstrike);
$output=array();

echo 'Strike 1: ';
var_dump($checkstrike['strike_1']);
echo '<br />Strike 2: ';
var_dump($checkstrike['strike_2']);
echo '<br />Strike 3: ';
var_dump($checkstrike['strike_3']);
On a side note, you added more X's to each field, which isn't what you want. Each $output element should only contain a single X.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

robshanks wrote:You need to put:

Code: Select all

.. 
$output=array("-","-","-");
Why do you keep filling your array with "-" ? This is not neccesary
User avatar
robshanks
Forum Commoner
Posts: 32
Joined: Sun Aug 05, 2007 9:27 pm
Location: Hull, UK

Post by robshanks »

robshanks wrote:
You need to put:
php:
..
$output=array("-","-","-");

Why do you keep filling your array with "-" ? This is not neccesary
In order to acheive an output of -X- if only field_2 has data:-
robshanks wrote:

Alternatively:
This will show X for fields with data in them (e.g. fields 1 and 3 empty 2 has data would display -X-):
Post Reply