Jump to content

Please help with echoing message


lingo5

Recommended Posts

Hi, I want some text message to print only on the first row of my query results but I don't know how to.

He's my query:

 

$maxRows_clientes_RS = 15;
$pageNum_clientes_RS = 0;
if (isset($_GET['pageNum_clientes_RS'])) {
  $pageNum_clientes_RS = $_GET['pageNum_clientes_RS'];
}
$startRow_clientes_RS = $pageNum_clientes_RS * $maxRows_clientes_RS;

mysql_select_db($database_MySQLconnect, $MySQLconnect);
$query_clientes_RS = "SELECT * FROM t_clientes WHERE cliente_iSclient = 1 ORDER BY company_name ASC";
$query_limit_clientes_RS = sprintf("%s LIMIT %d, %d", $query_clientes_RS, $startRow_clientes_RS, $maxRows_clientes_RS);
$clientes_RS = mysql_query($query_limit_clientes_RS, $MySQLconnect) or die(mysql_error());
$row_clientes_RS = mysql_fetch_assoc($clientes_RS);

if (isset($_GET['totalRows_clientes_RS'])) {
  $totalRows_clientes_RS = $_GET['totalRows_clientes_RS'];
} else {
  $all_clientes_RS = mysql_query($query_clientes_RS);
  $totalRows_clientes_RS = mysql_num_rows($all_clientes_RS);
}
$totalPages_clientes_RS = ceil($totalRows_clientes_RS/$maxRows_clientes_RS)-1;

 

and heres how I output my results:

 

<table width="850" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td>
<?php do { ?>
      <table width="100%" border="0" cellpadding="12" cellspacing="0" class="BottomLine">
        <tr >
          <td width="275" align="left" nowrap="nowrap" class="AddressTXT"><strong><?php echo $row_clientes_RS['company_name']; ?></strong></td>
          <td width="178" align="left" nowrap="nowrap" class="AddressTXT"><strong>tel.<?php echo $row_clientes_RS['cliente_tel']; ?></strong></td>
          <td width="237" align="left" nowrap="nowrap" class="AddressTXT"><strong>mov.<?php echo $row_clientes_RS['cliente_movil']; ?></strong></td>
          <td width="20" align="center" bgcolor="#FFFFFF" class="CP_errorLoginTxt"><a href="PC_clientes_update.php?id_cliente=<?php echo $row_clientes_RS['id_cliente']; ?>">edit<img src="CP_img/icons/edit.png" alt="" width="20" height="20" border="0" /></a></td>
          <td width="20" align="center" bgcolor="#FFFFFF" class="CP_errorLoginTxt"><a href="PC_clientes_delete.php?id_cliente=<?php echo $row_clientes_RS['id_cliente']; ?> "onClick="return confirm('¿Seguro que desea \n ELIMINAR PERMANENTEMENTE \n <?php echo $row_clientes_RS['company_name']; ?>?');">delete<img src="CP_img/icons/borrar.png" width="20" height="20" border="0" /></a></td>
        </tr>

      </table>
    <?php } while ($row_clientes_RS = mysql_fetch_assoc($clientes_RS)); ?></td>
  </tr>

</table>

 

Thanks!

Link to comment
Share on other sites

or

 

Don't use a while loop to loop through all of your results.

 

To clarify what he means, you don't have to use a while loop to return database rows. You only need to use a loop if you want to return more than one row.

 

To just return the first row, simply do this:

$row_clientes_RS = mysql_fetch_assoc($clientes_RS);

Link to comment
Share on other sites

Oh I see what you want now. You can simply use a boolean variable to output something only once. Here's a similar example.

 

<?php 

$array = array( 'one','two','three','four' );

$switch = TRUE;

foreach( $array as $value ) {
echo $value;
if( $switch == TRUE ) {
	echo ' this is only on first line';
	$switch = FALSE; // stops this conditional from executing again
}
echo '<br>';
}

?>

 

If you wanted more control, you could use a counter instead of a switch, and increment it at the end of every loop.

Link to comment
Share on other sites

Oh I see what you want now. You can simply use a boolean variable to output something only once. Here's a similar example.

 

<?php 

$array = array( 'one','two','three','four' );

$switch = TRUE;

foreach( $array as $value ) {
echo $value;
if( $switch == TRUE ) {
	echo ' this is only on first line';
	$switch = FALSE; // stops this conditional from executing again
}
echo '<br>';
}

?>

 

If you wanted more control, you could use a counter instead of a switch, and increment it at the end of every loop.

 

How is this any different than your first reply? Just seems like a way more complicated solution to me.

Link to comment
Share on other sites

How is this any different than your first reply? Just seems like a way more complicated solution to me.

 

What? He wants a list of results displayed in a table. When the first result is outputted only, he wanted to display additional text. This is nothing like my original post.

 

Sorry xyph, I understand the logic of it but still to advanced for me to apply it to my code

Could you elaborate for dummies?

 

It doesn't get much more simple than that. I've applied the logic to arrays, rather than a database so you don't just copy and paste the code and learn nothing. It's an extra variable, and 2 lines in a conditional statement within your loop. If you'd like help with a certain part of my example, feel free to ask. I'm not going to assist you in implementing it into the code you've pasted.

 

It makes me think the code you've posted is not your own, and you simply want help changing functionality of some else's code. That's not what this forum is for.

Link to comment
Share on other sites

How is this any different than your first reply? Just seems like a way more complicated solution to me.

 

What? He wants a list of results displayed in a table. When the first result is outputted only, he wanted to display additional text. This is nothing like my original post.

 

Yeah, I see that now. My mistake.

 

Link to comment
Share on other sites

scootstah, I have tried your suggestion using a test page like this:

 

<?php require_once('../Connections/MySQLconnect.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $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;
}
}

mysql_select_db($database_MySQLconnect, $MySQLconnect);
$query_test_RS = "SELECT * FROM t_clientes";
$test_RS = mysql_query($query_test_RS, $MySQLconnect) or die(mysql_error());
$row_test_RS = mysql_fetch_assoc($test_RS);
$totalRows_test_RS = mysql_num_rows($test_RS);
?>
<!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>
<?php do { ?>
  <table width="850" border="0" align="center" cellpadding="8" cellspacing="0">
    <tr>
      <td><?php echo $row_test_RS['company_name']; ?></td>
    </tr>
  </table>
<?php while ($row_test_RS = mysql_fetch_assoc($test_RS)) 
{
 if  (!isset($once)) 
 {     
 echo hello// your message here     
 $once = true;}}?>
</body>
</html>
<?php
mysql_free_result($test_RS);
?>

but I get a blank page... :confused:

Link to comment
Share on other sites

That's because you're using generated code. It's impossible to help you with this because you don't understand what we're helping you with.

 

If we simply show you where to put the code, the problem will be solved for you.

 

IMO You should simply uninstall Dreamweaver. It's a terrible way to both learn and implement PHP, and with CSS2, HTML has become more style-sheet tweaking than markup.

 

If you'd like to learn the language, I'm here to help. If you want to use generated code, perhaps you should ask Adobe to add that functionality to their editor.

Link to comment
Share on other sites

It's not helpful, it's enabling you to fling together behavior and code that you don't understand. Just because it 'works' doesn't mean it's ready for production.

 

And when you want to change BASIC behavior, you have no idea how to do it. You need to ask others to not only show you how it's done, but physically put the code in the right spot for you.

Link to comment
Share on other sites

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.