Jump to content

Trouble with arrays..


kyleabaker

Recommended Posts

First of all, I just want to point out that I'm very new to php and my code may not be the most ideal. ;)

 

I'm trying to make a better search page for my site that I've been slowly improving. I made a custom blog type of site just for fun I guess and I have a search page, but it's not working exactly how I want it to so I've taken up the task of rewriting it. So here is what I'm doing..

 

I'm splitting the search terms used into an array (works fine) and looping through the blog posts on my site searching for matches. If it finds a match then I add the number of matches for that word to a $hits variable and add the number for the next term and so on until all the words have been searched for that blog post. Then (the part that is giving me trouble) I am trying to loop through an array that I'm using to sort the results as I go according to the number of hits. I've tried several different things, but setting a specific array item doesn't seem to save as I have in my head. I guess I don't understand it well enough to think through it enough and catch the problem in my head, but I do understand how arrays work. Here is what I'm trying to do with the results that I collect..

 

(my reasoning and explanation is in the code, hopefully it helps get my goal across well enough)

//$searchhits          -> an array holding # of hits for each result so I can use it to figure out where to place the next results
//$searchresults      -> of course holds the results in the format that I want to echo out in the end
//$hits                   -> integer number, # of hits for the specific blog post to be added to the arrays
//$searchresultdata -> basic content from the blog post that I want to echo out for that result later

if (strlen($searchresults[0]) == 0) { //**if the array is blank just add to start and be done with this result
$searchhits[0]=$hits;
$searchresults[0]=$searchresultdata;
} else { //**else loop through the hits array and find the spot that this result should be inserted and shift everything else down
for ($j = count($searchresults)-1; $j > -1; $j--) { //starting at bottom so I can shift items down
	if ($searchhits[$j]<$hits) { //shift item down and clear item's previous spot (just trying NULL to see if it made a different, it didn't)
		$searchhits[$j+1]=$searchhits[$j];
		$searchresults[$j+1]=$searchresults[$j];
		$searchhits[$j]=NULL;
		$searchresults[$j]=NULL;
	} else { //from bottom up, I'm assuming j+1 is clear since I NULL it if anything was once there and if it was the last spot that was compared then that spot+1 should be empty to add to
		$searchhits[$j+1]=$hits;
		$searchresults[$j+1]=$searchresultdata;
	}
}
}

 

I'm fairly confident that the problem is hidden (to me) in the "else" inside the for loop, but I can't figure it out in my head why it won't work. I know you guys are gurus here, so this should be a piece of cake, haha. I'm looking for some simple code that I can understand by the way. ;D Thanks in advanced..and sorry if it's an easy thing to fix. I've looked over the php manuals online for a while trying to figure this out. ??? Feel free to simplify my code! Thanks!

Link to comment
Share on other sites

in the end of else part add line break;

		} else { //from bottom up, I'm assuming j+1 is clear since I NULL it if anything was once there and if it was the last spot that was compared then that spot+1 should be empty to add to
		$searchhits[$j+1]=$hits;
		$searchresults[$j+1]=$searchresultdata;
                        break;
	}

Link to comment
Share on other sites

Still having a bit of trouble, but I have this one pin-pointed, just don't know how to handle it.

 

Say I have the following number of hits in order that I find them:

1

1

1

1

3

 

My for loop is correcting shifting the entries for the first 4 down when it gets to a result with more hits, but I can't figure our how to insert it because my conditions aren't 100% correct. So when I run the search and it processes it shifts 4 down, but leaves the first result in position 1 followed by 1st,2nd,3rd,4th..

 

1

1

1

1

1

 

Here is what I've got now..

if (strlen($searchresults[0]) == 0) {
$searchhits[0]=$hits;
$searchresults[0]=$searchresultdata;
echo "(1) Post ID: ".$post_id." - Post Hits: ".$hits." - Array Count: ".count($searchresults)."<br>";
} else {
for ($j = count($searchresults)-1; $j >= 0; $j--) {
	if ($searchhits[$j]<$hits) {
		$searchhits[$j+1]=$searchhits[$j];
		$searchresults[$j+1]=$searchresults[$j];
		echo "(2) Post ID: ".$post_id." - Post Hits: ".$hits." - Array Count: ".count($searchresults)."<br>";
	} else {
		$searchhits[$j+1]=$hits;
		$searchresults[$j+1]=$searchresultdata;
		echo "(3) Post ID: ".$post_id." - Post Hits: ".$hits." - Array Count: ".count($searchresults)."<br>";
		break;
	}
}
}

 

and here is what it echoes..

(1) Post ID: 29 - Post Hits: 1 - Array Count: 1

(3) Post ID: 25 - Post Hits: 1 - Array Count: 2

(3) Post ID: 19 - Post Hits: 1 - Array Count: 3

(3) Post ID: 17 - Post Hits: 1 - Array Count: 4

(3) Post ID: 12 - Post Hits: 1 - Array Count: 5

(3) Post ID: 8 - Post Hits: 1 - Array Count: 6

(2) Post ID: 7 - Post Hits: 3 - Array Count: 7

(2) Post ID: 7 - Post Hits: 3 - Array Count: 7

(2) Post ID: 7 - Post Hits: 3 - Array Count: 7

(2) Post ID: 7 - Post Hits: 3 - Array Count: 7

(2) Post ID: 7 - Post Hits: 3 - Array Count: 7

(2) Post ID: 7 - Post Hits: 3 - Array Count: 7

 

Post ID is shifting everything down, but never caught to insert at position 0 in the array and I can't figure out how to catch it without catching all the others where $j is 0. Got any ideas?

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.