开发者

display data one by one page and at once using php

开发者 https://www.devze.com 2023-01-22 14:12 出处:网络
I have two mysql database tables: 1) CARS TABLE: with following columns i)car\'s name and ii)manufacturer\'s name.

I have two mysql database tables:

1) CARS TABLE: with following columns i)car's name and ii)manufacturer's name.

2) RECORDS TABLE: with the following columns i)name of the user ii)rating given iii)car's name.

Now i want the each user to give rating( rating will be a text input box) to each and every car in two ways.

1) ONE BY ONE: The user enters rating for the first car from the ca开发者_开发问答rs table. He clicks next and he goes to another page where he can enter the rating for the second car from the cars table, while the first rating is saved to the database. I am not able to figure out how to show the rating page one by one. Using $_SESSION?

2)ALL AT ONCE: If i want the user to enter the give ratings to all the cars at once then how do i identify each and every rating on the processing page once the user enters the rating and clicks submit?

thanks in advance.


1) Yes, you can use the session. You can store each rating in an array. Make sure to call session_start() in the first line of the page, before you output anything. For each page, you could do something like this:

//start using sessions
session_start();
//if the session variable is empty, create a new array
$votesArray = empty($_SESSION['votesArray']) ? array() : $_SESSION['votesArray'];
//push the submitted vote value
array_push($votesArray, $_POST['voteValue']);
//store the vote array in the session
$_SESSION['votesArray'] = $votesArray;

Then on the last page, you can get the entire vote array using $_SESSION['votesArray'], and do what you want with it.

2) This is a little more complex. Personally when I run into situations like this, I use indexing to build my form. Example:

<form method="post" action="">
    <?php
        //for each car, generate the form
        for($index=0;$index<$numberOfCars;$index++) {
    ?>
    <input name="vote<?php echo $index;?>" type="hidden" value="5">
    <?php
        }
    ?>
    <input type="submit">
    <!-- stores the number of votes that will be submitted -->
    <input name="numberOfVotes" type="hidden" value="<?php echo $numberOfCars; ?>">
</form>

This will output a hidden input for each car with a voting value in it. When the form is submitted, you will use this logic to get each vote:

<?php
    for($i=0;$i<$_POST['numberOfVotes'];$i++) {
        $vote = $_POST['vote'.$i]; //this is the i'th car's vote
    }
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消