Jump to content

Field value keeps repeating when special code is used


HowdeeDoodee

Recommended Posts

 

OK, here is the problem as best as I can describe it. Every page coming out of the db has 10 records displayed per page of the pagination script. The fields are Topic, Subtopic, Source, and References. The problem is the Subtopic field keeps repeating on every page beginning with the first record on each page. An illustration is below. For example, if the first record is about apples, the Subtopic refers to apples as the subtopic should. However, the next record on the page has a Topic showing oranges but the oranges record still has the Subtopic from apples. All the subtopics on page 1 have the same info showing the info related to apples. On page 2, the first record is water and the Subtopic is about water which is correct. However, all the other records on page 2 are showing Subtopics about water instead of being about the what the Subtopic is supposed to be about. Here is an illustration.

 

Page 1

Topic- Subtopic

Apples - Apples

Pears- Apples

Rabbits- Apples

Lettuce- Apples

Rats- Apples

Dogs- Apples

Fish- Apples

Pencils- Apples

CPUs- Apples

Computer - Apples

 

Page 2

Topic- Subtopic

Water- Water

Air- Water

Table- Water

Chair- Water

Moss- Water

Leaf- Water

Toads- Water

Simple- Water

Complex- Water

Fair- Water

 

The $Subtopic field is being repeated until a new page is brought up using the pagination script. Maybe a legitimate question would be, is there any way to refresh or activate the following line in the code shown below "$Subtopic = $result;" for every record, rather than refresh the code line for every page. $result is only being changed when the page changes instead of when the records change. Do I need to put the code beginning at the line ?$string = $Subtopic;" into a function and somehow call the function?

 

Here is the code

 

    

$result2 = mysql_query($query2) or die("The query $query2 failed. MySQL said: " . mysql_error());

    $Source="Source";
    $Topic="Topic";
    $Subtopic="Subtopic";
    $References="References";
while(list($Source, $Topic,$Subtopic,$References)= mysql_fetch_row($result2)) 
{
    if ($colorcounter == 0) {
    $colorbg = "#d8e0f0";    
    }
    else {
    $colorbg = "#e6eaf4";
    $colorcounter = $colorcounter - 2;
    }


$string = $Subtopic;

$terms = array($terms[0], $terms[1],$terms[2],$terms[3],$terms[4],$terms[5],$terms[6],);
$bgcolor = "yellow";
foreach($terms as $value) {
$count++;
if(!empty($value)) {
	$replaceWith = "<span style=\"background-color:$bgcolor;\">" . $value . "</span>";
	if($count==1) {
		$result = eregi_replace($value, $replaceWith, $string);
	} else {
		$result = eregi_replace($value, $replaceWith, $result);
	}
}
}

$Subtopic = $result;

 

No code exists below the code above except the html for the display of the page.

 

If I comment out the line "$Subtopic = $result;" there is not repeating of the Subtopic parts of the records displayed i.e., everything works normally. However, if I comment out this line, I get no highlighting on the script. So the issue is not whether the $Subtopic value is passed out of the db. The issue is how or why $result is called only once per page instead of once per record.

Link to comment
Share on other sites

Seems to me that every time you echo $subtopic, you are just echoing the one value that it was assigned from the code you displayed. If you want $subtopic to display a different $subtopic multiple times in the page, I would create a function that returns $subtopic and call that in the portion of the script that generates the tables. ex:

<?php
     
     function subtopic(desired parameters){
          //desired code here....
          //......
          return $subtopic;
     }

     //Run a query....
     //......

     echo 
            "<table>"
                   ."<th>Water</th><th>Subtopic</th>";
     
     while ($row = mysql_fetch_assoc($result)){
          $sbtopic = subtopic(desired params);
          echo
                "<tr><td>".$sbtopic."</td></tr>";

     }



?>

 

With the way you had it before, I think $subtopic was being assigned a value only once per script. If, every time you escape back into PHP, you call the subtopic function with new parameters, it will assign a new value to subtopic.

Link to comment
Share on other sites

Thank you thefortrees for the response. I am a total novice on the programming of this. I can understand it after it is written but to write it is beyond my sub-novice ability.

 

OK, the variables going into the code I posted earlier that replace or highlight words are...

 

$Subtopic

$terms[0]

$terms[1]

$terms[2]

$terms[3]

$terms[4]

$terms[5]

$terms[6]

 

So in the top of the function, do I put?

 

function subtopic($Subtopic, $terms[0], $terms[1]$terms[2]$terms[3]$terms[4]$terms[5]$terms[6]){

 

Then how do I call the function or get $Subtopic out of the function for every record on every page?

 

The html portion of the script that displays the table info taken from the db is just below the first script I posted at the top of this thread. The html code is as follows...

 

echo "<div align=left>\n";
echo "  <table border=0 width=745 height=10 td align=center cellspacing=0 cellpadding=\"0\">\n";
echo "      <td bgcolor=$colorbg valign=top align=left width=643 height=10% <b><i><font color=#FF0066 face=Arial size=2>Topic:</font><font face=Arial size=2 color=#000000>\n";
echo "        </font><b><font face=\"Arial\" color=\"#000000\" size=\"2\">$Topic</font></b></i></td>\n";

echo "      <td bgcolor=$colorbg valign=top align=left width=102 height=10% <b><i><font color=#FF0066 face=Arial size=2>Source:</font><font face=Arial size=2 color=#000000>\n";
echo "        <font size=2>$Source</font></i></td>\n";

echo "  <table border=0 width=745 height=10 td align=center cellspacing=0 cellpadding=\"0\">\n";
echo " <td align=center>\n";
echo "    <tr>\n";
echo "      <td bgcolor=$colorbg valign=top align=left width=745px height=10% <b colspan=\"2\"><i><font color=#FF0066 face=Arial size=2>Subtopic:<br></font><font face=Arial size=2 color=#000000>\n";
echo "        <font size=2>$Subtopic</font></td>\n";
echo "    </tr>\n";

echo "    <tr>\n";
echo "      <td bgcolor=$colorbg valign=top align=left width=745px height=10% <b colspan=\"2\"><i><font face=Arial size=1 color=#000000>\n";
echo "        <font size=1>$References</font></td>\n";
echo "    </tr>\n";
echo "  </table>\n";
echo "</div>\n";

print("<table style=\"table-layout:fixed\" border=1 align=center width=700 bgcolor=#0000c8 style=\"font-family: Arial; font-weight: bold\" cellpadding=\"1\" cellspacing=\"1\" font face=Arial><td align=center width=0px><font color=#FFFFFF><b></b></font></td><td align=center width=0px><font color=#FFFFFF><b></b></font></td><td align=center width=0px><font color=#FFFFFF><b></b></font></td></tr>");

 

Thank you for the help, it is really appreciated.

 

 

 

 

 

Link to comment
Share on other sites

so this might work...

 

function subtopic(){
       $terms = array($terms[0], $terms[1],$terms[2],$terms[3],$terms[4],$terms[5],$terms[6],);
       $bgcolor = "yellow";
       foreach($terms as $value) {
       $count++;
       if(!empty($value)) {
	       $replaceWith = "<span style=\"background-color:$bgcolor;\">" . $value . "</span>";
	       if($count==1) {
		       $result = eregi_replace($value, $replaceWith, $string);
	       } else {
		       $result = eregi_replace($value, $replaceWith, $result);
	          }
      }
      return $result;
}

Then whenever you want to call the function create a variable that is assigned the return value of the function subtopic() and echo it....

while ($row = mysql_fetch_assoc($queryResult){
     $sub = subtopic();
}

I can't guarantee this will work or that it is exactly what you want.

Link to comment
Share on other sites

Thank you for your response.

Well, now I am getting a blank screen except for top stuff indicating the pagination page links are working. The following line is the line below the last of the last query statements.

[code]$result2 = mysql_query($query2) or die("The query $query2 failed. MySQL said: " . mysql_error());

 

OK, I pasted the code in just as you see it here.

below.

 

$Source="Source";

    $Topic="Topic";

    $Subtopic="Subtopic";

    $References="References";

while(list($Source, $Topic,$Subtopic,$References)= mysql_fetch_row($result2))

{

    if ($colorcounter == 0) {

    $colorbg = "#d8e0f0";   

    }

    else {

    $colorbg = "#e6eaf4";

    $colorcounter = $colorcounter - 2;

    }[/code]

 

function subtopic($Subtopic, $terms)
	{
$string = $Subtopic;
$terms = array($terms[0], $terms[1],$terms[2],$terms[3],$terms[4],$terms[5],$terms[6],);
$bgcolor = "yellow";
foreach($terms as $value) {
$count++;
if(!empty($value)) {
	$replaceWith = "<span style=\"background-color:$bgcolor;\">" . $value . "</span>";
	if($count==1) {
		$result = eregi_replace($value, $replaceWith, $string);
	} else {
		$result = eregi_replace($value, $replaceWith, $result);
        }
}
}
	}
	{
      return $result;
	}

while ($row = mysql_fetch_assoc($queryResult){
     $sub = subtopic();
}

 

 

I am getting a parse error "Parse error: syntax error, unexpected '{' " pointing to the last bracket in the code below.

 

while ($row = mysql_fetch_assoc($queryResult){
     $sub = subtopic();
}

 

 

Question: Does the following code line go beneath the code as shown above or does the line go where it says...

 

"while(list($Source, $Topic,$Subtopic,$References)= mysql_fetch_row($result2)) " as shown above.

{

 

Thank you again for your responses.

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.