Jump to content

Recommended Posts

Hello guys and girls, im having issues running a conditional inside a loop. The virst include is working but the include wont loop even thought the values are? Hope this make sense to you guys.

 

Snippet area.

          <?php do { ?>
          <?php 
// Show IF Conditional region1 
if (@$row_Recordset1['tittleX_name'] != @$row_Recordset2['tittleX_name']) {
?>
                <?php echo $row_Recordset1['tittleX_name']; ?><br />
              <?php } 
// endif Conditional region1
?> 
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>

 

Page code


<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$KTColParam1_Recordset1 = "0";
if (isset($_GET["temp_id"])) {
  $KTColParam1_Recordset1 = $_GET["temp_id"];
}
mysql_select_db($database_CORE, $CORE);
$query_Recordset1 = sprintf("SELECT tittleX.tittlex_weight, tittleX.tittleX_id, tittleX.tittleX_temp_id, tittleX.tittleX_hint, tittleX.tittleX_name FROM tittleX WHERE tittleX.tittleX_temp_id=%s ORDER BY tittleX.tittlex_weight ASC ", GetSQLValueString($KTColParam1_Recordset1, "int"));
$Recordset1 = mysql_query($query_Recordset1, $CORE) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);

$KTColParam1_Recordset2 = "0";
if (isset($_GET["boat_id"])) {
  $KTColParam1_Recordset2 = $_GET["boat_id"];
}
mysql_select_db($database_CORE, $CORE);
$query_Recordset2 = sprintf("SELECT tittleX.tittleX_temp_id, tittleX.tittleX_name, fieldX.fieldX_address_id, fieldX.fieldX_id, tittleX.tittleX_id, fieldX.fieldX_tittle_id, tittleX.tittlex_weight, tittleX.tittleX_hint, fieldX.fieldX_value FROM (tittleX LEFT JOIN fieldX ON fieldX.fieldX_tittle_id=tittleX.tittleX_id) WHERE fieldX.fieldX_address_id=%s ORDER BY tittleX.tittlex_weight ASC ", GetSQLValueString($KTColParam1_Recordset2, "int"));
$Recordset2 = mysql_query($query_Recordset2, $CORE) or die(mysql_error());
$row_Recordset2 = mysql_fetch_assoc($Recordset2);
$totalRows_Recordset2 = mysql_num_rows($Recordset2);
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<body>
<table width="100%" border="0" cellpadding="20">
  <tr>
   
   
    <td valign="top">
         
         
         
          <?php do { ?>
          
          
          
          <?php 
// Show IF Conditional region1 
if (@$row_Recordset1['tittleX_name'] != @$row_Recordset2['tittleX_name']) {
?>
                <?php echo $row_Recordset1['tittleX_name']; ?><br />
              <?php } 
// endif Conditional region1
?> 

<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
   
   
   
   
   
  </td>
    
    
    
    
    
    <td valign="top"><?php do { ?>
        <?php echo $row_Recordset2['tittleX_name']; ?><br />
    <?php } while ($row_Recordset2 = mysql_fetch_assoc($Recordset2)); ?></td>
  </tr>
</table>
</body>
</html>
<?php
mysql_free_result($Recordset1);

mysql_free_result($Recordset2);
?>

 

This is probable something simple but i cant seam to figure it out. Please help.

 

I will buy anyone who can fix this a pint.

You could store the results of 1 query into an array and then use the in_array function. Heres an example.

 

$query1 = mysql_query("SELECT * FROM table1 WHERE field1='xyz'");
$temp = array();
while($row = mysql_fetch_assoc($query1)) {
 $temp[] = $row['title'];
}

$query2 = mysql_query("SELECT * FROM table2 WHERE field1='abc'");
while($row = mysql_fetch_assoc($query2)) {
 if(!in_array($row['title'], $temp)) {
    print $row['title']."<br />\n";
 }
}

 

Ideally I would only use 1 query and rewrite it so I only return the results I require. However in your case I do not know your database design so this is a dirty method.

Thanks, Neil. My db will not allow me to create this as one query as the items dont exist in the first query results, they are related to virtual tables which only exist in the second query. This probably makes little or no sense to you since im not good at explain my logic.

 

Would you be willing to show me how to do this for a fee?

$query2 = mysql_query("SELECT * FROM table2 WHERE field1='abc'");
while($row = mysql_fetch_assoc($query2)) {
  if(!in_array($row['title'], $temp)) {
     print $row['title']."<br />\n";
  }
}

 

Neil, regarding your code... how about if we want to search only from 1 table with conditional statement...

 

i just want to use

 

  if(!in_array($row['title'], $temp)) {
     print $row['title']."<br />\n";
  } else {
     echo ("no data on your query");
  }

 

is it possible? ..

No, your issue is different. Im guessing your just after if the query returns 0 rows. Here's how:

if(!mysql_num_rows($query)) {
  echo ("no data on your query");
}
else {
   while($row = mysql_fetch_assoc($query)) {
   
   }
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.