3

I am trying to store values from form radio to array. But problem which i am facing is that every time array 1st index is replaced with new value. I believe this is scope problem. I also tried to declare global array but no success.

Here is the code:

<?php
include_once("connection.php");
$c = new DBcon();
$c->startcon();
global $array; // not effecive
//  $q = $_POST['t'];
//  echo 'fff', $q;


$page = $_GET['page'];
echo 'pagesss', $page, 'ppp';
if ($page == "") {

    $page = "1";
} else {

    // If page is set, let's get it
    $page = $_GET['page'];
}

// Now lets get all messages from your database
$sql = "SELECT * FROM quizes";
$query = mysql_query($sql);

// Lets count all messages
$num = mysql_num_rows($query);

// Lets set how many messages we want to display
$per_page = "2";

// Now we must calculate the last page
$last_page = $num;

echo 's', $num;

// And set the first page
$first_page = "1";

// Here we are making the "First page" link
echo "<a href='?page=" . $first_page . "'>First page</a> ";

// If page is 1 then remove link from "Previous" word
if ($page == $first_page) {

    echo "Previous ";
} else {

    if (!isset($page)) {

        echo "Previous ";
    } else {

        // But if page is set and it's not 1.. Lets add link to previous word to take us back by one page
        $previous = $page - 1;

        echo "<a href='?page=" . $previous . "'>Previous</a> ";
    }
}

// If the page is last page.. lets remove "Next" link
if ($page == $last_page) {

    echo "Next ";
} else {

    // If page is not set or it is set and it's not the last page.. lets add link to this word so we can go to the next page
    if (!isset($page)) {

        $next = $first_page + 1;
        echo "<a href='?page=" . $next . "'>Next</a> ";
    } else {

        $next = $page + 1;
        echo "<a href='?page=" . $next . "'>Next</a> ";
    }
}

// And now lets add the "Last page" link
echo "<a href='?page=" . $last_page . "'>Last page</a>";

// Math.. It gets us the start number of message that will be displayed
$start = ($page * ($page - 1)) / $page;
echo 'start', $start;
echo 'page', $page;
// Now lets set the limit for our query
$limit = "LIMIT $start, $per_page";

// It's time for getting our messages
$sql = "SELECT * FROM quizes $limit";

$query = mysql_query($sql);

echo "<br /><br />";

// And lets display our messages

$i = 0;
$l = 0;

while ($row = mysql_fetch_array($query) or die(mysql_error())) {
    $a = $row['A'];
    echo '<form method="get" action="?page=".$next."">';
    while ($row = mysql_fetch_array($query)) {

        echo '<div class="boxed" >';

        echo "\t" . '<tr><th>' .
        $row['question'] . "<br>" .
        '</th><th>' . "<input type='radio' name= 't[]' value='{$row['A']}'>" . $row['A'] . "<br>" .
        '</th><th>' . "<input type='radio' name='t[]' value='{$row['B']}'>" . $row['B'] . "<br>" .
        '</th><th>' . "<input type='radio' name='t[]' value='{$row['C']}'>" . $row['C'] . "<br>" .
        '</th><th>' . "<input type='radio' name='t[]' value='{$row['D']}'>" . $row['D'] . '</th>
                </tr>';
        echo '<input type="hidden" name="page" value="' . $next . '">';
        echo '<input type="submit" name="submit"/>';
        $i++;

        echo '</div>';


        echo '</div>';
    }
    echo '</form>';
    if (isset($_GET['submit'])) {
        $example = $_GET['t'];
        foreach ($example as $value) {
            $array[$i++] = ($value);

            echo "$array[0] <br>"; // printing correct statement but replacing old values with new value everytime.
            echo "$array[1] <br>"; // 0 values
            echo "$array[2] <br>"; // 0 values
        }
    }
}
?>

I have seen these posts: PHP array indexing: $array[$index] vs $array["$index"] vs $array["{$index}"] , PHP - define static array of objects but no help. Kindly help what should i do?

6
  • One thing, instead of doing a mysql_row_count at the beginning just do a query like: "SELECT count(*) FROM quizes" since it will only return one value, otherwise you transfer more data than necessary to get the same value from the database. Second do a var_dump of the $example value to see what you get as a response to your form. Commented Jan 13, 2016 at 7:13
  • u mean i shd add extra query? Commented Jan 13, 2016 at 7:17
  • var_dump yeah but i am unable to add values in array. Instead it is only adding and replacing value on 1st index not on others. Commented Jan 13, 2016 at 7:20
  • Make sure you print out the $i value too in the loop to be sure that it is setting the value correctly. Commented Jan 13, 2016 at 7:26
  • i have written count query in this way: $count = "SELECT count(*) FROM quizes "; $cnt = mysql_query($count); echo 'count',$cnt; is it wrong? Commented Jan 13, 2016 at 7:32

4 Answers 4

2

You cannot do

$array[$i++]

this will always be $array[1] instead do

$i++;
$array[$i]= $value;

P.S. $array is a terrible name for a variable...

Sign up to request clarification or add additional context in comments.

1 Comment

i did but value is storing in the 1st index and replacing old value with new in 1st index
1
if (isset($_GET['submit'])) {
        $example = $_GET['t'];
$i=0;
$arrayA = array();
        foreach ($example as $value) {
            $arrayA[$i] = ($value);
$i++;

        }
print_r($arrayA);
    }

Comments

0

if you do

$array[$i++] ;

it wont work , just try this and it wont replace your old value

$array[$i] = $value;    

    $i++;

Comments

0

You must increment your variable first, then use the variable as the array key.

Edit: this is what you're looking for:

$i = 0; 

foreach ($example as $value) {
    $array[$i] = $value;    

    $i++;
}

3 Comments

how? is nt like c++?
actually $i++ does not return true, it returns $i. $i=10; var_dump($i++) will output 10 not true
same nothing change. If u see my code:- if (isset($_GET['submit'])) { $l++; $example = $_GET['t']; foreach ($example as $value) { $array[$l] = ($value); echo 'l',$l,"$array[$l] <br>"; $l++; } they all are in 1st while loop and as a result i am stuck in scope and thats why i am unable to add values in array, Can u suggest ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.