Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If or alternative statement? 1

Status
Not open for further replies.

keepingbusy

Programmer
Apr 9, 2000
1,470
GB

Hi

Some coding contained in a php file on our site has the following code:
Code:
case "text":
  echo "<tr>";
  echo "<td valign='top'><strong>".$field['Title']."</strong></td>";
  echo "<td>";
  if($field['IsLink'] == 'yes'){
    echo "<a target='".$field['Target']."' href='".$listing[$field['Title']]."'>".$field['LinkText']."</a>";	
  }else
    echo $listing[$field['Title']];
I am fairly new to php but I think I understand the mechanics of this section where it is asking for the field name to be displayed <td valign='top'><strong>".$field['Title']. and then the field itself $listing[$field['Title']]

I am hoping my question is easy for you guys.

If I do not want a particular field name or field names to appear but just the content of the field to appear what do I need to add or do to the above code?

An example of this could be: The field name is Summary. I do not want the field name shown but a blank space instead but I still want the contents of the summary field to show.

Some guidance would be appreciated.

Many thanks
KB
 
Should we understand that by fields you mean DB field?

In order to help you further, please show us the query and how you generate $field
Remember to x-out any real db name, user name, password etc...
 
if i understand you correctly, something like this should work.
Code:
<?php
	$blankFieldNames = array(	'summary', 
								'somethingelse'
							); //insert all the fields that you don't want headings for.  lower case only
	switch (something) {
	//	...
		
	case "text":
		echo "<tr>";
		if (in_array(strtolower($field['Title']), $blankFieldNames)){
			$title = '&nbsp;'; //insert a non breaking space to keepthe table neat
		} else {
			//it's not a field name we want blank so echo the proper field title
			$title = $field['Title'];
		}
		echo "<td valign='top'><strong>$title</strong></td>\r\n";
		echo "<td>\r\n";
		if($field['IsLink'] == 'yes'){
			echo "<a target='".$field['Target']."' href='".$listing[$field['Title']]."'>".$field['LinkText']."</a>";    
		}else {
			echo $listing[$field['Title']];
		}
		// .... more stuff here
?>
 

webdev007

Should we understand that by fields you mean DB field?
Yes. My apologies, I didn't make it clear.

jpadie

Tried out your suggestion and an error came back as:
Code:
Parse error: syntax error, unexpected T_WHILE, expecting T_CASE or T_DEFAULT or '}' in /homepages/xx/d1xxxxxxxx/htdocs/xxxxxxxx/detail.php on line 144

KB
 
that error means that you have mistyped something or otherwise got your syntax wrong. we cannot help you debug it without you providing the code that you are using.
 

Hi jpadie

Does this help:
Code:
}

while( $field = mysql_fetch_assoc($fieldsRS) ){
  if($field['Type'] != "Mapping"){
    switch($field['Type']){
    case "text":
        echo "<tr>";
        echo "<td valign='top'><strong>".$field['Title']."</strong></td>";
        echo "<td>";
        if($field['IsLink'] == 'yes'){
          echo "<a target='".$field['Target']."' href='".$listing[$field['Title']]."'>".$field['LinkText']."</a>";	
            }else
          echo $listing[$field['Title']];
        break;
    case "textarea":
        echo "<tr>";
        echo "<td valign='top'><strong>".$field['Title']."</strong></td>";
        echo "<td>";
        echo nl2br($listing[$field['Title']]);
        break;
Thank you
KB
 
No, not really. Your error is reported on the line 144 and there's only about 20 lines here. According to the error, you probably have a mismatch of opening and closing braces. You should make sure you have the closing brace for the switch statement, if statement and while statement at the end of this block. I believe you're probably missing a set of these.

___________________________________________________________
[small]Do something about world cancer today: Comprehensive cancer control information at PACT[/small]
 
You probably need an editor that highlights braces and parentheses
Please, show your code up to the end of the "while" brace
 
Code:
while( $field = mysql_fetch_assoc($fieldsRS) ){
  if($field['Type'] != "Mapping"){
  switch($field['Type']){

  case "text":
    echo "<tr>";
    echo "<td valign='top'><strong>".$field['Title']."</strong></td>";
    echo "<td>";
    if($field['IsLink'] == 'yes'){
    echo "<a target='".$field['Target']."' href='".$listing[$field['Title']]."'>".$field['LinkText']."</a>";	
    }else
    echo $listing[$field['Title']];

    break;

  case "textarea":
    echo "<tr>";
    echo "<td valign='top'><strong>".$field['Title']."</strong></td>";
    echo "<td>";
    echo nl2br($listing[$field['Title']]);
    break;
  case "select":
    echo "<tr>";
    echo "<td valign='top'><strong>".$field['Title']."</strong></td>";
    echo "<td>";						
    echo $listing[$field['Title']];
    break;
  case "checkbox":
    echo "<tr>";
    echo "<td valign='top'><strong>".$field['Title']."</strong></td>";
    echo "<td>";					
				
    $checkboxlist = $listing[$field['Title']];
    $checkboxArray = @explode(',',$checkboxlist);
    $num_checkboxs = count($checkboxArray);
				
    $count = 0;
    $break = ceil($num_checkboxs/2);
					
    echo "<table border=0 cellpadding=1 cellspacing=0 width='100%'>";
    echo "<tr>";
    echo "<td valign='top'>";
				
foreach($checkboxArray as $value){
      echo $value;
      echo "<br/>";
      if($count == $break)
      echo "</td><td valign='top'>";
      $count++;
    }

    echo "</td>";
    echo "</tr>";
    echo "</table>";
	
    break;			
  }
  echo "</tr>";
  }
}
Thank you

KB
 
you haven't provided the code as webdev007 requested. again, please provide ALL code up until the end of the while segment.
 
Here is the remainder, after which is all the coding is html and will not affect the above
Code:
include("xxxxxxxx/xxxxxxxx-xxxxxxx.php");
$sql = "SELECT * FROM `".$TABLES["XXXXXXXXX"]."`
  WHERE username='".intval($_REQUEST["fieldname"])."'
  AND password='".intval($_REQUEST["fieldname2"])."'";
  $sql_result = mysql_query ($sql, $connection ) or die 
  ('request "Could not execute SQL query" '.$sql);
  $xxxxxxx = mysql_fetch_assoc($sql_result);
  if ($UserCal['id']>0) {

  //ECHO xxxxxxxx
  echo "<p>";
  echo "<table class='detailTable' cellspacing=0 border=0 width='100%'>";
  echo "<tr>";
  echo "<td class='subHeader'>xxxxxxxxxxxx</td>";
  echo "<tr>";
  echo "<td>";
?>
Thank you
 
Hi keepingbusy,

Code:
$field = mysql_fetch_assoc($fieldsRS)
Means $field is an associative array (that is an array, which is not adreessed by an element index but by an associated name, eg 'Title' or 'IsLink') with a record from the current resultset $fieldsRS. Each field value is stored inside $field[] associated to it's field name.

$field['Title'] therefore is a value of the table field Title of the queried table. $field['Title'] is not, like you interpret it a PHP function to get the field name or title. More generally speaking, $field is not an array of field properties of a single field of a row of the resultset, but an array of field values of the whole current row of a resultset.

So what is displayed as the title is purely data driven here. Put &nbsp; inside that 'Title' field of your underlying table and a nonbreakable space space will be shown as the title.

Bye, Olaf.
 
I understand that you want to keep the posted portions of code as short as possible. But as long as there is not the full post one can only guess which line is line number 144. So at least give the first line numbers of each code segment you posted...

Bye, Olaf.
 

Hello Olaf

I'm sorry my friend, whilst I am grateful for your valued post, that has gone completely over the top of my head. I was thinking my question would have been quite simple to resolve, but it seems not.

Thanks again, but for the sake of hiding two field names on our site, I'll give it a miss for now.

Thank you all for your posts.

KB
 
Hi KB,

sorry, if that is too much for you. Here's an explaination of mysql_fetch_assoc:
In the sample code you can see how it is used and that it does NOT print the field names, but field values:

Code:
while ($row = mysql_fetch_assoc($result)) {
    echo $row["userid"];
    echo $row["fullname"];
    echo $row["userstatus"];
}

If you see technical field names they may have come into the data, eg you imported a text file with it's first line containing field names/captions and thus have the field names inside the fields as table data. Your code does not seem to show technical field names. Perhaps take a look at the raw data in $TABLES["XXXXXXXXX"].

Bye, Olaf.
 
Looks like some very good information here, but applying it to my situation is sadly over my head.
In the following chunk of code, if $zen is 0 I want to display No in the list, otherwise I want to display Yes. Something along the lines of Iff($zen,yes,no)? I just have no idea how to plug that into this situation.

Code:
//Get all program info
	$get_detail_sql = "SELECT * from v_program_details 
		where prognum = '".$_POST["sel_id"]."'";
	$get_detail_rs = mysqli_query($conn, $get_detail_sql)
			or die(mysqli_error($conn));
	if (mysqli_num_rows($get_detail_rs) > 0) {
 
	while ($detail_info = mysqli_fetch_array($get_detail_rs)) {
	$org = stripslashes($detail_info['organization']);
	$loc = stripslashes($detail_info['locname']);
	$ip = stripslashes($detail_info['ip_block']);
	$zen = stripslashes($detail_info['zen']);
	$dns = stripslashes($detail_info['dns']);
	$low = stripslashes($detail_info['lowgrade']);
	$high = stripslashes($detail_info['highgrade']);
		$display_block .="<li>$org
				<li>Grades $low - $high
				<li>Located at $loc
				<li>ip block = $ip
				<li>DNS = $dns
				<li>Zen site: $zen
				<li>$dns</li>";
	}
	$display_block .="</ul>";

If you could provide a brief explanation or reference, that would be great. I really need to understand this too.
Thanks for all the help, I couldn't get this done without you.
 

Hi StuartBombay

I was hoping the iif or if/then/else or if/else/endif statements would be allowed in this set up but it doesn't look like it's that easy!

Lets wait and see what other responses we get.

KB
 
In response to Stuart:
Code:
$org = stripslashes($detail_info['organization']);
    $loc = stripslashes($detail_info['locname']);
    $ip = stripslashes($detail_info['ip_block']);
    $zen = stripslashes($detail_info['zen']);
    [red]if($zen==0){$zen="NO";} else{$zen="YES";}[/red]
    $dns = stripslashes($detail_info['dns']);
    $low = stripslashes($detail_info['lowgrade']);
    $high = stripslashes($detail_info['highgrade']);

The red section should do what you want.







----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top