Login   Register  
PHP Classes
elePHPant
Icontem

adjust value in array key for a field matched by an input string

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us

      Top level forums  >  PHP Specialists  >  General  >  adjust value in array key for a field matched by an input string  
Subject:adjust value in array key for a field matched by an input string
Summary:php 2d array
Messages:2
Author:jeff ferguson
Date:2013-12-17 15:23:05
Update:2013-12-19 16:38:05
 

  1. adjust value in array key for a field matched by an input string   Reply  
Picture of jeff ferguson
jeff ferguson
2013-12-17 15:23:05
I've made a processing sketch that works in a web browser that accesses ,sorts, and saves csv lists in the web server by sending queries to the php script. It gets the sorted lists back and displays them. (I don't want to get into xml or sql until later)

Now I am trying to make it possible to vote on the entries in the list.

In my code snippet below, I have a query come in from processing that votes on an entry by sending the string.

I would like to check the entry string passed from processing against the fields in the array, and adjust the score by adding one vote to it.

The code below does not break my other running code, but also does not adjust the votes. I suspect there is some way I am not accessing the string value correctly, and wonder if anyone could advise me....

$ListV[]='score';
$ListV[]='entry';

<Snip>

if ($type == "voteUp") {
$Entry = $_GET['Entry'];
if(($handle = fopen("List.csv", 'r')) !== FALSE) {
set_time_limit(0);
while(($data = fgetcsv($handle,0,',')) !== FALSE) {
$ListV[$row]=$data;
$row++;
}
fclose ($handle);

$count = count($ListV);
for($i=0; $i<$count; $i++){
if ($rListV[$i][1]==$Entry || strcmp($rListV[$i][1],$Entry)==0) {
$score =int($rListV[$i][0]);
$rListV[$i][0]=$score+1;
}
}
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=file.csv');

$listVote = fopen("List.csv",'w');
foreach ($ListV as $values) {fputcsv($listVote,$values);}
fclose($listVote);
}
}
I believe I have constructed a viable 2d Array with score and entry fields. The reason I put score first is so I can take advantage of key sorting and also use the array indexing (such as it is in php) when needed.

Please note, I am not trying to change the index with the voting in this list, just the score. So results should be as below;

Before voting:

2157 Kittenz
2157 Dogz
11 Antelopez

After voting for Dogz;

2157 Kittenz
2158 Dogz
11 Antelopez

So, Kittenz is always index 1, Dogz is always index 2, and so on.... (the key sorting gives a sorted array later)

There is 1 reply in this thread, which is not being displayed.
Browsing this forum thread replies is available only to premium subscribers.


Go to the premium subscriptions page to learn how to become a premium subscriber and have full access to this forum.