Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pointer control: array row with key

Status
Not open for further replies.

torodre

Technical User
Nov 8, 2008
7
0
0
US
Any help might be enough to point me in the right direction,
Thanks
In a multi-dimensional array--
What code is used to get the next row say if you start at [0] => 4[id] =>4 and end at [0] => 8[id] =>8 ? ( I am not sure if this detail describes my goal, 'pointer control')
to leave and come back at the next row? variables?
How does the fetch/query syntax look?
this was generated with print_r($row); there are 8 rows -
Array (
[0] => 1 [id] => 1
[1] => 2 [questionID] => 2
[2] => You are standing the wheelwatch when you hear the cry _Man overboard starboard side_. You should: [question] =>

You are standing the wheelwatch when you hear the cry _Man overboard starboard side_. You should:
[3] => give full right rudder [A] => give full right rudder
[4] => give full left rudder => give full left rudder
[5] => put the rudder amidships [C] => put the rudder amidships
[6] => throw a life ring to mark the spot [D] => throw a life ring to mark the spot
)

I want to pointer control, which row in an array 6 col by 8 rows or more rows
I have 100 questions I want to take blocks of 20 questions at a time, then radio button choice A -> D to get next question.

//This worked to get first question.

/// connection to db above this
if (!$_POST) {

$row = mysql_fetch_array($result);
include ("questionTable.inc");
include ("inputForm.inc");
}

// This gave me the rest, but ALL the rest

if ($_POST) {

While ($row = mysql_fetch_array($result)){
//
if(next($row)){
include ("questionTable.inc");
include ("inputForm.inc");
}
}
}
?>

////
inputForm.inc
////
<?php

echo "<CENTER>";
echo "<br /><br /><br />";
echo
"<form name='f' form method = 'POST' >.
<Input name = 'RadioAn' type ='radio' value = 'A'>A.
<Input name = 'RadioAn' type ='radio' value = 'B'>B.
<Input name = 'RadioAn' type ='radio' value = 'C'>C.
<Input name = 'RadioAn' type ='radio' value = 'D'>D.
<Input name = 'a' type ='hidden' value = '$a'>\n"; // $a might be used to control pointer?

echo "<BR><BR>";
echo "<input type='submit' value='Submit Answer'><BR></FORM>\n";

echo "</CENTER>";

?>
 
a multidimensional array will not normally look like that which you have provided. you have not shown us how the array is created.

a small multidimensional array will look like this

Code:
Array
(
    [0] => Array
        (
            [0] => cat
            [id] => feline
        )

    [1] => Array
        (
            [0] => doc
            [id] => canine
        )

)

you can see that each array element within the top array, is associated uniquely with a key.

So if i wanted to address the 'dog' element I could do this

Code:
echo "The animal $array[1][0] is in the family named $array[1][id]";

you can loop over arrays using the foreach construct

Code:
foreach ($array as $a){
  echo "The animal $a[0] is in the family named $a[id]";
}

and you can, of course, nest your foreach constructs.

additionally you can move through an array manually with the functions

Code:
next();
end();
prev();
reset();

there are many other ways of navigating an array too. look at the manual here
 
New Question2

Hi. Thanks for responding, Maybe I do not need an array loop maybe the direct approach, WHERE id = 7. I have been reading and trying for a month. I started a month ago. Maybe I still need to understand array [][] things? Maybe the answer is in the array call format/definition?

This:
$result = mysql_query("SELECT id,questionID,question,A,B,C,D FROM qTwo_tbl WHERE id = 7"); // this worked for this question.

how do i write/define the php query/fetch to have the id be in the 'where id='$id' similar to above, WHERE id = 7 ?
..................................................
THERE IS THIS EXAMPLE THAT WORKS IN AN OTHER PROGRAM CODE, i would like to understand. ie.

$query = "SELECT petName FROM Color
WHERE petName='$petName'
AND petColor='$petColor'";
...................................................
WHEN I TRIED THIS....
$cxn = mysqli_connect($host,$user,$password,$database)
or die ("Couldn't connect to server");

$query = "SELECT * FROM qTwo_tbl
ORDER BY id";

$result = mysqli_query($cxn,$query)
or die ("Couldn't execute query.");

$id == 6; // this didn't have any influence

$query = "SELECT id,questionID,question,A,B,C,D FROM qTwo_tbl // if id='$id' ... 'no row id' is printed
WHERE id=7 "; // if id=7 the question and radios are printed, this is good.
$result = mysqli_query($cxn,$query)
or die("Couldn't execute query.");


if ($row = mysqli_fetch_array($result))
{
include ("questionTable.inc");
include ("inputForm.inc");
}
else {echo " no row id ";
}

mysql_close($cxn);
?>

$query = "SELECT id,questionID,question,A,B,C,D FROM qTwo_tbl
WHERE id='$id'"; how?

How to get it to work with a variable? to match question index in db to called question?

loop format I tried....seems to be wrong direction.

//.................................................................................these loop
$row= mysql_fetch_array($result);

for($i=0; $i < count($row); $i++) { //
if($row[$i][0] == $x) { //
echo $row[$i][1]."\n";

//....................................................................................

while ($row = mysql_fetch_row($result))
{
$count = count($row);
$y = 0;
while ($y < $count)
{
echo current($row) . ' ';
next($row);
$y = $y + 1;
}
echo '<br>';
..................................................................................
Many Thanks, torodre
 
Lets start with your example:

Code:
 $query = "SELECT petName FROM Color
                     WHERE petName='$petName'
                     AND petColor='$petColor'";
[/color]

In this example $petName and $petColor are PHP variables, where they are populated I don't know.

In your  attempt you don't use any variables anywhere. 

[code]
 $query = "SELECT * FROM qTwo_tbl
                  ORDER BY id";

No variables.

This:
Code:
$id == 6;          // this didn't have any influence

Is wrong. 2 or more = signs are only used for comparisn, not assingment. So it would nbeed to be:

$id = 6;

Then in your query attempt:

Code:
   $query = "SELECT id,questionID,question,A,B,C,D FROM qTwo_tbl WHERE id=$id ";        


Should produce what you want. 

Now I'm t sure how all this relates to arrays, this is just simple variable usage.



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
RESOLVED

Thanks for your help. Since I am unknowing I could not even ask a well organized question, but with your help both of you I found a solution. Thanks. This has been the only place I received an answer.

Your help got me over my first hurdle:

$y = $y + 1;
echo $y . " this is y plus 1 after cycle trough form.";

$query = "SELECT id,questionID,question,A,B,C,D FROM qTwo_tbl WHERE id ='$y'";
$result = mysqli_query($cxn,$query)
or die("Couldn't execute query.");


if ($row = mysqli_fetch_array($result))
{
include ("questionTable.inc");
include ("inputForm.inc");
}
else {echo " no db feedback ";
}
Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top