Page 1 of 2

if something exists display X

Posted: Mon Aug 06, 2007 9:25 am
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.

standard if else

Posted: Mon Aug 06, 2007 9:55 am
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';

Posted: Mon Aug 06, 2007 9:56 am
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 =  '';
}

Posted: Mon Aug 06, 2007 10:07 am
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>""
?>

Posted: Mon Aug 06, 2007 11:20 am
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?

Posted: Mon Aug 06, 2007 11:43 am
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) 

Posted: Mon Aug 06, 2007 11:47 am
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.

Posted: Mon Aug 06, 2007 11:51 am
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?

Posted: Mon Aug 06, 2007 11:56 am
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>";

Posted: Mon Aug 06, 2007 11:59 am
by Zoxive
str_split() is a php5 function, most likely you don't have php5.

Posted: Mon Aug 06, 2007 12:08 pm
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! :)

Posted: Mon Aug 06, 2007 12:14 pm
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";
}

Posted: Mon Aug 06, 2007 12:17 pm
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.

Posted: Mon Aug 06, 2007 12:17 pm
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

Posted: Mon Aug 06, 2007 12:21 pm
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-):