Jump to content

Recommended Posts

  • Replies 115
  • Created
  • Last Reply

Top Posters In This Topic

Why don't you take a day or two off from coding and stay with your girls? :P

 

I wish I could mate but deadlines are not moving. the girls know I'm doing it for them.

 

sorry to ask but I would love some more clues. so when you said "question[0] would be line 1" I was assuming you suggested a variable for each line. you mean an array for each line right ?

 

is it true that   $questions[]= $data; gives an array for the whole file which is not what I want ?

 

thank you

is it true that   $questions[]= $data; gives an array for the whole file which is not what I want ?

 

It is true, but why wouldn't you want that?

 

Let's think for a while.

 

We should now have a code, that reads all lines from CSV file into $questions array, where $questions[0] is the first line, $questions[1] is the second etc... In other words $questions is an array of arrays...

 

Now, when your script is run for the first time, it should take first line ($questions[0]), and display question and answers form.

When the scripts is called with answer from the form posted, it should take line from $questions associated with the answer, and display its question and answers.

Rinse and repeat :)

 

What do you think?

 

Well have to look at the pros and cons of each routine

 

1) $questions contains all the items.

  • the file parsing is easier to code
  • the search code is a bit harder to code
  • Multidimensional array

 

2) $questions contains just the item needed

  • the file parsing is harder to code as it has the search parsing as well
  • No search code needed
  • Single array

 

and since the page reloads, ya may consider doing the search within the file parsing.

How is

1) $questions contains all the items.

  • the file parsing is easier to code
 
Extremely easy.

the search code is a bit harder to code
 
How is that? The array is indexed by line numbers, and we use line numbers to get items from it. No search at all.
 

Multidimensional array

Again, I fail to see it as a flaw. That's just two dimensions. Nothing really complicated.

member mchl, when the users done his work and thanks u,

 

can u kindly write a quick tutorial on the process on this grate help your teaching the other user...

 

im following all this for fun and enjoying it..

 

it grate to learn new ways off doing things good work mshl

member mchl, when the users done his work and thanks u,

 

can u kindly write a quick tutorial on the process on this grate help your teaching the other user...

 

im following all this for fun and enjoying it..

 

it grate to learn new ways off doing things good work mshl

 

I'll think about it... It could actually be a good subject for a small tutorial.

Sessions would be used here, to store user's answers until he gets to the last question. Once there, the answers should beretrieved from session and stored to file/to a database/diplayed/whatever.

 

There's already a tutorial about sessions on phpfreaks.

Hello everyone. I managed to claw some time back. Shes been a good baby for the past 8 days phew. still tiring though. :D

 

 

 

<?php

$file = fopen("questions.txt", "r");
while (($data = fgetcsv($file, 1000, ",")) !== FALSE) 
{
   $questions[]= $data;
}
fclose($file);

print_r($question[6][1]);  // <---- see here - a multidimensional array. 
//First number says which line from csv, 
//second number says which column from csv

?>

 

Thank you again for your support. yes i was playing around with the script the multidimensional array works: But isn't the page supposed to reload ?

So if im to use

<h1>Q: <?php echo $questions[0][1]; ?></h1>

  Isnt always going to be that questions whether I reload or submit a page ?

Look into rand

 

That may help you, and insert into the array definition for the first array, just make sure the top most number is the last array key or else you will get errors half the time.

 

<?php
$max = count($questions) - 1; // minus 1 since arrays are zero based.

$randIndex = rand(0, $max);
?>
<h1>Q: <?php echo $questions[$randIndex][1]; ?></h1>

 

Hope that helps ya.

I been pulling my hair out, getting stressed all night over this. I just can't seem to get it. Tried googling similar terms like " Read first line in array" and many others but they only seem to give results like

echo $questions[0][1]; ?>

I know this is not what I want.

 

I know for sure that one of the echo for the actual question will be something like

$quest[1] and answers $quest[2]  $quest[4]

 

I just have no idea how to "make" my script read the first line first without using $questions[0][1] for the question. I understand exactly what your saying Mchl but I just dont know where to look for such a function. I don't even know what its called.

 

For real this is not me crying for anyone to do the work for me as I will get caught for "contract cheating". My degree depends on this project. I am working hard, trying to think of ways (I've already thought of the (no choice left) way to do this by using

$question1 = $questions[0];
   $question2 = $questions[1];
   $question3 = $questions[2];
   $question4 = $questions[3];
   $question5 = $questions[4];
   $question6 = $questions[5];
   $question7 = $questions[6];

just incase I can't do this  :-[ but to no avail. Sound like I'm about to cry lol, I know

 

Are there any examples on the web that I don't know about ?

Thank you. Your a life saver !

Well have to look at the pros and cons of each routine

 

1) $questions contains all the items.

  • the file parsing is easier to code
  • the search code is a bit harder to code
  • Multidimensional array

 

2) $questions contains just the item needed

  • the file parsing is harder to code as it has the search parsing as well
  • No search code needed
  • Single array

 

and since the page reloads, ya may consider doing the search within the file parsing.

 

This was one of the reasons for my post, multi-dimensional arrays are good but ya need to know how to work with them.

as well as the file parsed may not begin with 1 as the index, it could begin with 412. which means finding the correct array becomes a search routine.

 

From the looks of it, you definately need a guide on working with arrays. and that is wut u shud google, not a quick fix for the code. In order to understand how arrays work, and thus yer own knowledge ya should google: using arrays php guide

 

And how about this?

 

$i = 1;
echo $questions[$i][1];
$i = 2;
echo $questions[$i][1];

 

Yup. You can use variables, to indicate array indexes.

You just have to find out, how to get (!) proper value into $i :P

$i = 1;
echo $questions[$i][1];
$i = 2;
echo $questions[$i][1];

 

 

Yes that has helped quite alot. I gotta say its exciting to learn new things about php ! Thank you. I've been testing here and there

 

$i = 0;
$i = 1;
$i = 2;

 

I have notived that php seems to only load 2 into the variable $i as its the last one. I've left it as $i = 0 as this is the first line i want to display. So I think (correct me if this is the wrong method) i should leave it as $i = 0 for the script to load first time. Saying that this will always load $i = 0.

 

Now I realise that when a user clicks the submit button and using the variable VALUE from the form, is it right or possible to, depending on the value choses (ie would be eg <? php $questions[$i][5]; ?>" so if its line 2 (in index term line 1) is it possible to somehow change the original value of $i = 0 to $i = 1 ? or is this where sessions comes into play ?

 

Maybe im thinking ahead of myself or I just got it totally wrong. But from what the testing has given me by declaring the variables like so

 

$i = 0;
$i = 1;
$i = 2;

shows that it wont work cause its just going to 'forget' $i = 0; $i = 1;

 

I hope that all made sense (trying to speak php language)  ;D

 

You're thinking quite right.

Any variable remembers only the last value assigned to it.

 

And yes... since you want to load first line when script starts, it should be set to 0.

Then you have to capture data from form... remember those $_POST and $_GET variables? This is their five minutes in your script!

This is their five minutes in your script!

 

Thanks for speedy reply mate !

 

How do you mean five minutes in script ?. As soon as the post or get is submitted I only have five mins. Sorry abit confused :D

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.