arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
squirto28
Forum Newbie
Posts: 11
Joined: Mon Mar 22, 2004 9:41 pm

arrays

Post by squirto28 »

I am trying to do a multidementional array from a query. Right now the echo statement of $array just prints Array a bunch of times. From the query I want to append a number to it so I can display it on the next page in a table. I don't know if I named the text field correctly. Any help or ideas????

Thanks

Code: Select all

<?php


for ($i=0; $i<$totalRows_Recordset1; $i++)
{ 
    $row_Recordset1 = mysql_fetch_array($Recordset1);
    echo "<tr>";
    echo "<td>";
    echo $row_Recordset1[Item_Name];
    echo "</td>";
for ($t=0; $t <= 3; $t++)
{    

    echo "<td>";
    echo "<input name = $row_Recordset1[Item_Code][$t] type='text'>";
    echo "</td>";
    $array = array( array($row_Recordset1[Item_Code], $t));
    echo  $array;
//    echo $t;
}
}

 




?>
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Instead of
$array = array( array($row_Recordset1[Item_Code], $t));
echo $array;

it sounds like you want ...
$itemcode = $row_Recordset1['Item_Code'].$t;
echo $itemcode;
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post by andre_c »

try this:

Code: Select all

echo "<input name = {$row_Recordset1[Item_Code][$t]} type='text'>";

// or

echo "<input name = " . $row_Recordset1[Item_Code][$t] . " type='text'>";
... i think that works
squirto28
Forum Newbie
Posts: 11
Joined: Mon Mar 22, 2004 9:41 pm

Post by squirto28 »

Thanks markl999, it worked.
squirto28
Forum Newbie
Posts: 11
Joined: Mon Mar 22, 2004 9:41 pm

Post by squirto28 »

:?:
That worked for the naming of the input box. but the user will enter numbers in there and them hit a submit button and I am going to perform a calculation with those numbers and display what they input as well as the result of the calculation. I cannot get the values I enter to show on the next page.

Any ideas??

Thanks
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

In which case i'd just do :
echo '<input name="numbers[]" type="text">';

Then on the page you post to you can do,
if(!empty($_POST['numbers'])){
foreach($numbers as $number){
echo $number; //for testing
//do the calculations here
}
}
squirto28
Forum Newbie
Posts: 11
Joined: Mon Mar 22, 2004 9:41 pm

Post by squirto28 »

This puts together all the numbers I input as just 1 string in each of the cells of the table. And all the cells have the same value.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

It shouldn't do as $_POST['numbers'] is an array.
Got some example code?
squirto28
Forum Newbie
Posts: 11
Joined: Mon Mar 22, 2004 9:41 pm

Post by squirto28 »

<?php require_once('Connections/connection.php'); ?>
<title>Missing Analysis Input Screen</title>
<form name="MissingAnalysisInputScreen2.php" method="post" >
<?
mysql_select_db($database_connection, $connection);
$query_Recordset1 = "SELECT * FROM Item_Master WHERE Item_Type = 'Supplies'";
$Recordset1 = mysql_query($query_Recordset1, $connection) or die(mysql_error());
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>

<table width="" border="1" align="center">
<tr>
<th>Item Name</th>
<th>Open</th>
<th>Stocked</th>
<th>Sold</th>
<th>Close</th>
<th>Missing</th>
</tr>
<?
for ($i=0; $i<$totalRows_Recordset1; $i++)
{
$row_Recordset1 = mysql_fetch_array($Recordset1);
echo "<tr>";
echo "<td>";
echo $row_Recordset1[Item_Name];
echo "</td>";
for ($t=0; $t <= 3; $t++)
{
echo "<td>";
if(!empty($_POST['numbers'])){
foreach($numbers as $number){
echo $number;
//for testing
//do the calculations here
echo "</td>";

}
}
}
}
mysql_free_result($Recordset1);
?>
</tr></table></td>
<p align="center">
<input type="submit" name="Submit" value="Back"><input type="submit" name="Submit" value="Next">
</p>
</form>
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Ah, i see what you're trying to do, you've put the code i gave you in the wrong place, the foreach() was supposed to be on the page you post to, not in the form. The form should be like:

echo "<td>";
echo '<input name ="number_'.$t.'[]" type="text">';
echo "</td>";

almost the same as you had it originally. But on the page that processes this form, ie the page you post TO you can do something like this to get at the values posted.
foreach($_POST as $key=>$val){
if(substr($key,0,6) == 'number'){
$num = end(explode('_', $key));
echo 'Store '.$num.' = '.$val[0];
echo 'Open '.$num.' = '.$val[1];
echo 'Close '.$num.' = '.$val[2];
echo '<br />';
}
}

You'll want to format this output to suite your needs, but it should at least show you how to get at the values they enter.
squirto28
Forum Newbie
Posts: 11
Joined: Mon Mar 22, 2004 9:41 pm

Post by squirto28 »

didn't work??

I tried various ways and changed the things but it doesn't retrieve the correct values.
this is what i did.

Code: Select all

<?php
for ($i=0; $i<$totalRows_Recordset1; $i++){ 
	$row_Recordset1 = mysql_fetch_array($Recordset1);
	echo "<tr>";
	echo "<td>";
	echo $row_Recordset1[Item_Name];
	echo "</td>";
	echo "</tr>";

	foreach($_POST as $key=>$val){    
	 if(substr($key,0,6) == 'number'){ 
	 $num = end(explode('_', $key));
	echo "<td>";
	echo 'Open '.$num.' = '.$val[0]; 
	echo "</td>";
	echo "<td>";
	echo 'Stocked'.$num.' = '.$val[1];
	echo "</td>";
	echo "<td>"; 
	echo 'Sold '.$num.' = '.$val[2]; 
	echo "</td>";
	echo "<td>"; 
	echo 'Close'.$num.' = '.$val[3]; 
	echo "</td>";
//echo '<br />'; 
} 
} 
}

?>

it does it for 0-3 but it puts it on the same line and just repeats it all the way down. It kinda works.
I tried various ways but still haven't been able to come up with a solution.

[/img]
Post Reply