Jump to content

nooby array question


Perad

Recommended Posts

I am still getting my head around arrays. I have this function, when i echo out the array values it only records the last value.
[code]
function reorder () {
$query = "SELECT group_name FROM uncgroupss";
$result = mysql_query ($query);
while ($row = mysql_fetch_assoc ($result)) {
$name = $row['group_name'];
$valuearray[] = $_POST[$name];
}
echo $valuearray[1];
echo $valuearray[2];
echo $valuearray[3];
}[/code]

I must be copying over the original value in the while statement. How can i tweak this to make it work?
Link to comment
Share on other sites

The index of arrays starts at zero not one. Do you know you have at least 3 records that will be selected by your query? If you just want to dump the values in the array, use the print_r() function.

[code]<?php
function reorder () {
$query = "SELECT group_name FROM uncgroupss";
$result = mysql_query ($query);
while ($row = mysql_fetch_assoc ($result)) {
$name = $row['group_name'];
$valuearray[] = $_POST[$name];
}
                echo '<pre>' . print_r($valuearray,true) . '</pre>';
        }
?>[/code]

Ken
Link to comment
Share on other sites

This is to make an order system for my website, I was trying to make a script which moved things up and down but found it to hard when people added and removed new groups.

In this it will print out each group with an order number. When you submit it i will check that all numbers are consecutive and not the same then i will enter the new orders into my database.

I am just having troubles storing all the values in the array.

[code]
<?
function ordergroup () {

function arrange (){
echo '<form action="admin_members.php' . '?action=reorder" method="post">';
$membername = $_POST['membername'];
$query = "SELECT * FROM uncgroupss ORDER BY group_order";
$result = mysql_query ($query);
while ($row = mysql_fetch_assoc ($result)) {
$groupname = $row['group_name'];
$grouporder = $row['group_order'];
echo 'Name: '.$groupname.' :: Current Order ::&nbsp;<input name="'.$groupname.'" type="text" size="3" maxlength="3" value="'.$grouporder.'" /><br />';
}
echo '<input type="submit" name="submit3" value="Search"></form>';
}

function reorder () {
$numbercount = 0;
$query = "SELECT group_name FROM uncgroupss ORDER BY group_order";
$result = mysql_query ($query);
while ($row = mysql_fetch_assoc ($result)) {
$name = $row['group_name'];
$valuearray[] = $_POST[$name];
$namearray[] = $name;
$numbercount++;
}
  echo '<pre>' . print_r($valuearray,true) . '</pre>';
}





switch($_GET['action']) {
case 'arrange':
arrange();
break;
case 'reorder':
reorder();
break;
default:
arrange();
}
}?>[/code]
Link to comment
Share on other sites

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.