lingo5 Posted December 3, 2011 Share Posted December 3, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/252395-please-help-with-echoing-message/ Share on other sites More sharing options...
xyph Posted December 3, 2011 Share Posted December 3, 2011 LIMIT 1 at the end of your query. or Don't use a while loop to loop through all of your results. Quote Link to comment https://forums.phpfreaks.com/topic/252395-please-help-with-echoing-message/#findComment-1294008 Share on other sites More sharing options...
lingo5 Posted December 3, 2011 Author Share Posted December 3, 2011 Thanks xyph, lets say I use the LIMIT 1 at the end of my query...how do I echo the message I want on row 1? Quote Link to comment https://forums.phpfreaks.com/topic/252395-please-help-with-echoing-message/#findComment-1294010 Share on other sites More sharing options...
scootstah Posted December 3, 2011 Share Posted December 3, 2011 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); Quote Link to comment https://forums.phpfreaks.com/topic/252395-please-help-with-echoing-message/#findComment-1294012 Share on other sites More sharing options...
xyph Posted December 3, 2011 Share Posted December 3, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/252395-please-help-with-echoing-message/#findComment-1294014 Share on other sites More sharing options...
scootstah Posted December 3, 2011 Share Posted December 3, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/252395-please-help-with-echoing-message/#findComment-1294021 Share on other sites More sharing options...
lingo5 Posted December 3, 2011 Author Share Posted December 3, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/252395-please-help-with-echoing-message/#findComment-1294022 Share on other sites More sharing options...
lingo5 Posted December 3, 2011 Author Share Posted December 3, 2011 Thanks scootstah but I want to output ll the rows..I just need to print a message on row 1. Quote Link to comment https://forums.phpfreaks.com/topic/252395-please-help-with-echoing-message/#findComment-1294026 Share on other sites More sharing options...
scootstah Posted December 3, 2011 Share Posted December 3, 2011 Oh, I gotcha. Well then sort of going off xyph's response, try this: while ($row_clientes_RS = mysql_fetch_assoc($clientes_RS)) { if (!isset($once)) { // your message here $once = true; } } Quote Link to comment https://forums.phpfreaks.com/topic/252395-please-help-with-echoing-message/#findComment-1294028 Share on other sites More sharing options...
xyph Posted December 3, 2011 Share Posted December 3, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/252395-please-help-with-echoing-message/#findComment-1294032 Share on other sites More sharing options...
scootstah Posted December 3, 2011 Share Posted December 3, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/252395-please-help-with-echoing-message/#findComment-1294035 Share on other sites More sharing options...
lingo5 Posted December 3, 2011 Author Share Posted December 3, 2011 xyph the code is mine..but it was generated by DW as I am still not confident enough to hard code it myself. I have learned a lot since I found this board...but I steel need help sometimes. Quote Link to comment https://forums.phpfreaks.com/topic/252395-please-help-with-echoing-message/#findComment-1294037 Share on other sites More sharing options...
scootstah Posted December 3, 2011 Share Posted December 3, 2011 Did you try my suggestion? Quote Link to comment https://forums.phpfreaks.com/topic/252395-please-help-with-echoing-message/#findComment-1294039 Share on other sites More sharing options...
lingo5 Posted December 3, 2011 Author Share Posted December 3, 2011 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... Quote Link to comment https://forums.phpfreaks.com/topic/252395-please-help-with-echoing-message/#findComment-1294063 Share on other sites More sharing options...
lingo5 Posted December 3, 2011 Author Share Posted December 3, 2011 I mean, no message is echoed when I use your code. Quote Link to comment https://forums.phpfreaks.com/topic/252395-please-help-with-echoing-message/#findComment-1294077 Share on other sites More sharing options...
xyph Posted December 3, 2011 Share Posted December 3, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/252395-please-help-with-echoing-message/#findComment-1294081 Share on other sites More sharing options...
lingo5 Posted December 3, 2011 Author Share Posted December 3, 2011 I have started this project using DW because there is plenty of complicated stuff I can't do with my actual level of PHP knowledge. DW is probably crap but it is really helpful for me at this stage. Thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/252395-please-help-with-echoing-message/#findComment-1294092 Share on other sites More sharing options...
xyph Posted December 3, 2011 Share Posted December 3, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/252395-please-help-with-echoing-message/#findComment-1294096 Share on other sites More sharing options...
lingo5 Posted December 4, 2011 Author Share Posted December 4, 2011 xyph what editor you recommend? Quote Link to comment https://forums.phpfreaks.com/topic/252395-please-help-with-echoing-message/#findComment-1294280 Share on other sites More sharing options...
xyph Posted December 5, 2011 Share Posted December 5, 2011 I use Eclipse PDT myself, but there are tons of great ones out there. http://www.phpfreaks.com/forums/index.php?topic=277416.0 Quote Link to comment https://forums.phpfreaks.com/topic/252395-please-help-with-echoing-message/#findComment-1294708 Share on other sites More sharing options...
lingo5 Posted December 5, 2011 Author Share Posted December 5, 2011 Thanks!! Quote Link to comment https://forums.phpfreaks.com/topic/252395-please-help-with-echoing-message/#findComment-1294711 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.