Jump to content

Recommended Posts

Newbie question. I've searched a bunch on IF else logic and while I get close I don't see how to resolve this. Combine that with poor algebra skills and you get a question like this!

 

I have code that was written for me that I'm editing. The page in question may return 100 or more results. About every 10 rows (or whatever) I'd like to add a row (TR) that contains a link like "Back to Top" of page.

 

Further up in my code, we establish the number of rows:

....
$result=mysql_query($query);
$num=mysql_numrows($result); 
mysql_close();

 

Then in my loop to setup and echo results:

<?php
$i=0;
while ($i < $num) {
$Active=mysql_result($result,$i,"Active");
//ETC, ETC...then...I print the rows:
<tr> 
<td valign="top"><? echo "$id"; ?></td>
</tr>
//ETC, ETC...then...I advance the counter:
<?php
++$i;
} 
echo "</table><P><A href=\"#top\">Back to top</A></P></B><BR>";
?>

This puts "Back to Top" at the bottom of the page, which is fine.

 

So, just to see if I could see if the ROW was on number five, I inserted this in the loop:

<?php
if($num = "5")  echo "<TR><TD colpan=\"6\"><P><A href=\"#top\">Back to top</A></P><TD><TR>"; 
++$i;
} 
echo "</table><P><A href=\"#top\">Back to top</A></P></B><BR>";
?>

 

But I get the row on every record. So how do I determine if the row number is divisible by five AND echo the row correctly? I'm also getting just one cell, not the span I'm looking for.

Link to comment
https://forums.phpfreaks.com/topic/84934-solved-if-else-logic/
Share on other sites

It would be....

 

<?php
if ($i == 5) {
  echo "<TR><TD colpan=\"6\"><P><A href=\"#top\">Back to top</A></P><TD><TR>"; 
  $i++;
} else {
  echo "</table><P><A href=\"#top\">Back to top</A></P></B><BR>";
}
?>

 

And to make it happen every 10.

 

[code]
<?php
if ($i % 10) {
  echo "<TR><TD colpan=\"6\"><P><A href=\"#top\">Back to top</A></P><TD><TR>"; 
  $i++;
} else {
  echo "</table><P><A href=\"#top\">Back to top</A></P></B><BR>";
}
?>

[/code]

Link to comment
https://forums.phpfreaks.com/topic/84934-solved-if-else-logic/#findComment-433046
Share on other sites

Thorpe,

 

Thanks for the reply. Using your code for "every 10" broke the page; in that nothing displays now. I corrected the TD and TR not being closed, but that still doesn't work.

 

Same on the "finding 5th row" (but quite frankly I don't need this. It was just my first feeble attempts at trying on my own.

Link to comment
https://forums.phpfreaks.com/topic/84934-solved-if-else-logic/#findComment-433059
Share on other sites

thorpe just edited your if statement. You should've erased the </table> in the last echo and put the </table> back at the bottom later.

 

<?php
if ($i % 10) {
  echo "<TR><TD colpan=\"6\"><P><A href=\"#top\">Back to top</A></P><TD><TR>"; 
  $i++;
} else {
  echo "<B><P><A href=\"#top\">Back to top</A></P></B><BR>";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/84934-solved-if-else-logic/#findComment-433065
Share on other sites

Ken2k7-

 

Thanks. Again, my page breaks and nothing displays. I noticed the closing slashes are missing in the "answer". Am I missing something then, or shouldn't those be there?

 

Again, I'ma newbie, but I want to advance the counter regardless of what row I'm on. When I read this last post, I feel like we're trying to find row 10 AND advance if it is within that "IF". In this case we'll never get to 10 will we?

Link to comment
https://forums.phpfreaks.com/topic/84934-solved-if-else-logic/#findComment-433070
Share on other sites

Sorry, I was hoping not to make everyone wade through a whole bunch of code that might not have mattered.

<?php
include("dbinfo.inc.php");
$tDay = date("m-d");
$fromDay = $_REQUEST["f_date"];
$toDay = $_REQUEST["t_date"];
mysql_connect(mysql,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
if (!empty($_REQUEST["t_date"])) {
  $query='SELECT *,DATE_FORMAT(Cbirthdate,"%m-%d") AS rbirthdate FROM birthdays where Event="Birthday" AND DATE_FORMAT(Cbirthdate,"%m-%d") >= "'.$fromDay.'" AND DATE_FORMAT(Cbirthdate,"%m-%d") <= "'.$toDay.'"';
} else if (!empty($_REQUEST["showall"])) {
  $query='SELECT *,DATE_FORMAT(Cbirthdate,"%m-%d") AS rbirthdate FROM birthdays where Event="Birthday"';
} else if (!empty($_REQUEST["id"])) {
  $query='SELECT *,DATE_FORMAT(Cbirthdate,"%m-%d") AS rbirthdate FROM birthdays where Event="Birthday" AND id='.$_REQUEST["id"];  
} else {
  $query='SELECT *,DATE_FORMAT(Cbirthdate,"%m-%d") AS rbirthdate FROM birthdays where Event="Birthday" AND DATE_FORMAT(Cbirthdate,"%m-%d") = "'.$tDay.'"';
}

  

$result=mysql_query($query);

$num=mysql_numrows($result); 

mysql_close();

echo "<A name=\"top\"></A><b><center><H1>Martini In The Morning Birthday Club Members<FONT color=\"#ff0000\"><BR><I>By Referer</I></FONT></H1></b>";
?>
<!--  -->

<?php
if (!empty($_REQUEST["id"])) {
   echo "<center><FONT color=\"#ff0000\"><B>Record Updated</B></FONT></center>";
}
if (!empty($_REQUEST["del"])) {
   echo "<center><FONT color=\"#ff0000\"><B>Record Deleted</B></FONT></center>";
}
?>
<center><div style='width:100%; background-color:silver; text-align:right'>
  <?php SECShowAdminLink(); ?>
   
  <?php SECShowLogoutLink(); ?>  <A href="data.php">Main Data Page</A>  
</div></center>
<center><div style='width:100%; background-color:#ffe4c4; text-align:center'><BR>Today's birthdays have been pre-selected<BR><A href="birthdays2.php">View by Referred</A> | <A href="birthdays_active.php">Active Records Only</A> | <A href="birthdays_inactive.php">InActive Records Only</A><BR><BR></div></center>
<form name="form1" method="post">
<table border="0" cellspacing="2" cellpadding="2" width="800">
      <TR>
  	   <TD valign="top">
            <table cellspacing="0" cellpadding="0" style="border-collapse: collapse"><tr>
             <td><input type="text" name="f_date" id="f_date" readonly="1" /></td>
             <td><img src="JS/CalendarImage.gif" id="f_trigger" style="cursor: pointer; border: 1px solid red;" title="Date selector"
                  onmouseover="this.style.background='red';" onmouseout="this.style.background=''" /></td>
            </table>
            
            <script type="text/javascript">
                Calendar.setup({
                    inputField     :    "f_date",     // id of the input field
                    ifFormat       :    "%m-%d",      // format of the input field
                    button         :    "f_trigger",  // trigger for the calendar (button ID)
                    align          :    "Tl",           // alignment (defaults to "Bl")
                    singleClick    :    true
                });
            </script>
	   </TD>
	   	  	   <TD valign="top">
            <table cellspacing="0" cellpadding="0" style="border-collapse: collapse"><tr>
             <td><input type="text" name="t_date" id="t_date" readonly="1" /></td>
             <td><img src="JS/CalendarImage.gif" id="t_trigger" style="cursor: pointer; border: 1px solid red;" title="Date selector"
                  onmouseover="this.style.background='red';" onmouseout="this.style.background=''" /></td>
            </table>
            </form>
            
            <script type="text/javascript">
                Calendar.setup({
                    inputField     :    "t_date",     // id of the input field
                    ifFormat       :    "%m-%d",      // format of the input field
                    button         :    "t_trigger",  // trigger for the calendar (button ID)
                    align          :    "Tl",           // alignment (defaults to "Bl")
                    singleClick    :    true
                });
            </script>
	   </TD>	   
            <TD valign="top"><input type="submit" name="Submit" value="Submit"></TD>
            <TD valign="top"> <A href="birthdays.php?showall=1">Select ALL Birthdays</A></TD> 
	   <TD valign="top"> <A href="birthdays.php">Select Today</A></TD>		   
      </TR>	  
</TABLE>
<CENTER><P><?php echo "Showing: $fromDay to $toDay"; ?> </P></CENTER>
<center><div style='width:100%; background-color:#eee; text-align:right'>
  <?php SECShowAdminLink(); ?>
   
  <?php SECShowLogoutLink(); ?>
</div></center>
<table border="1" cellspacing="2" cellpadding="2" width="800" class="sortable">
<tr> 
<th width="10">DB #</th>
<th width="250">Name/Address</th>
<th width="100">Country</th>
<th width="200">E-mail</th>
<th>D.O.B.</th>
<th>Active</th>
</tr>

<?php
$i=0;
while ($i < $num) {
$Active=mysql_result($result,$i,"Active");
$firstname=mysql_result($result,$i,"Contact_Info_FirstName");
$lastname=mysql_result($result,$i,"Contact_Info_LastName");
$street=mysql_result($result,$i,"Contact_Info_StreetAddress");
$street2=mysql_result($result,$i,"Contact_Info_Address2");
$city=mysql_result($result,$i,"Contact_Info_City");
$state=mysql_result($result,$i,"Contact_Info_State");
$zip=mysql_result($result,$i,"Contact_Info_ZipCode");
$country=mysql_result($result,$i,"Contact_Info_Country");
$email=mysql_result($result,$i,"Contact_Info_Email");
$Cbirthdate=mysql_result($result,$i,"Cbirthdate");
$Bfirstname=mysql_result($result,$i,"Birthday_Info_FirstName");
$Blastname=mysql_result($result,$i,"Birthday_Info_LastName");
$Bstreet=mysql_result($result,$i,"Birthday_Info_StreetAddress");
$Bstreet2=mysql_result($result,$i,"Birthday_Info_Address2");
$Bcity=mysql_result($result,$i,"Birthday_Info_City");
$Bstate=mysql_result($result,$i,"Birthday_Info_State");
$Bzip=mysql_result($result,$i,"Birthday_Info_ZipCode");
$Bcountry=mysql_result($result,$i,"Birthday_Info_Country");
$Bemail=mysql_result($result,$i,"Birthday_Info_Email");
$Bbirthdate=mysql_result($result,$i,"Bbirthdate"); 
$id=mysql_result($result,$i,"id"); 
$Bdate = date("m-d-Y",strtotime("$Bbirthdate"));
$Cdate = date("m-d-Y",strtotime("$Cbirthdate"));
?>

<tr> 
<td valign="top"><? echo "$id"; ?></td>
<td valign="top"><STRONG><? echo "$firstname $lastname"; ?></STRONG><BR><? echo "$street  $street2<BR>$city, $state $zip<BR><B>  <SMALL>------------ Referred... -----------</SMALL></B><BR>-"; ?>
<? echo "$Bfirstname $Blastname"; ?><BR><? echo "$Bstreet  $Bstreet2<BR>$Bcity, $Bstate $Bzip"; ?></td>

<td valign="top"><CENTER><? echo "$country"; ?></CENTER><BR><BR><HR width="85%"><CENTER><? echo "$Bcountry"; ?></CENTER></td>
<td valign="top"><CENTER><a href="mailto:<? echo "$email"; ?>"><? echo "$email"; ?></a></CENTER><BR><BR><HR width="85%"><CENTER><a href="mailto:<? echo "$Bemail"; ?>"><? echo "$Bemail"; ?></a></CENTER></td>
<td valign="top"><? echo "$Cdate"; ?><BR><BR><BR><HR width="85%"><? echo "$Bdate"; ?></td>
<td valign="top"><CENTER><?php    $checked = $Active ? "checked" : "";
echo "<input type=\"checkbox\" name=\"foo\" $checked>";
?>
 <BR><BR><a href='birthdays_edit.php?id=<? echo "$id"; ?>'>edit</a> </CENTER></TD>
</tr>
<?php
++$i;
} 
echo "</table><P><A href=\"#top\">Back to top</A></P></B><BR>";
?>

<BR><BR><BR>
           <?php include "include/footer.php"; ?>	
</BODY>
</html>

Link to comment
https://forums.phpfreaks.com/topic/84934-solved-if-else-logic/#findComment-433076
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.