Link to home
Start Free TrialLog in
Avatar of adamn123
adamn123

asked on

putting array into array

I have array of this

        $l[] = "<tr><td>".mssql_result($resulta, $ia, 'location')."</td>
            <td>".mssql_result($resulta, $ia, 'MC9063')."</td>
            <td>".mssql_result($resulta, $ia, 'MC9090')."</td>
            <td>".mssql_result($resulta, $ia, 'MC9060')."</td>
            <td>".mssql_result($resulta, $ia, 'MC3090')."</td>
            <td>".mssql_result($resulta, $ia, 'MC9060G')."</td></tr>";

I have another array of this
        $b[] = mssql_result($result, $i, 'Description');

Which would output -> MC9063MC9090MC9060MC3090MC9060G


I need to make my array $l more dynamic and less static becuase the Description will very

I made a thrid aray bellow
        $c[] = "<td>.mssql_result($"."resulta, $"."ia, '".mssql_result($result, $i, 'Description')."').</td>";

my goal is this

        $l[] = "<tr><td>".mssql_result($resulta, $ia, 'location')."</td>". implode($c) .   " </tr>";


The Problem I am having is the output in my table is this
locationabc
.mssql_result($resulta, $ia, 'MC9063').
.mssql_result($resulta, $ia, 'MC9090').
.mssql_result($resulta, $ia, 'MC9060').
etc

It is putting text instead of doing the php


I can not get around this.
I am hoping you understand what I am trying to do.
Avatar of deefjuh
deefjuh

This must be some complex table you are trying to create.

After reading it thoroughly I came with a (not so elegant) solution.

The third array you describe:

"<td>.mssql_result($"."resulta, $"."ia, '".mssql_result($result, $i, 'Description')."').</td>";
Is in fact a literal: the right mssql_result is outside the "" after the dot, so its's concatenated.

So my guess is:
You tried to construct a new function call with all the parameters dynamically.
You used the $"."ia construction because otherwise it got evaluated (it probably gave errors, but because you made it a string with " instead of ' it will evaluate the variable).

Aren't you trying to achieve:
            "<td>".mssql_result($resulta, $ia, mssql_result($result, $i, 'Description'))."</td>";

If it is not the solution ( I have no clue what you are trying to achieve) a few tips:
"" --> for string where variables will be printen
'' --> nothing willl be evaluated
count ( and ) : functionscalls need to be closed always

Let me know

Avatar of adamn123

ASKER

Belllow is the full code.

Maybe it will help make more sense
$query = "select Distinct m.ID, m.Description
from conf_Inventory_Model m
      join Inventory i
        on m.ID=i.ID_Model
      join conf_Company c
        on i.ID_Company=c.ID
where c.Company='$usernew' and
      substring(m.Description, 1, 2)= 'MC'";
 
 
$result = mssql_query($query);
 
$a = array();
$b = array();
$c = array();
$l = array();
 
mt_srand((double)microtime()*1000000);
for($i = 0; $i < mssql_num_rows($result); $i++)
{
 
        $a[] = "Sum(Case when i.ID_Model=".mssql_result($result, $i, 'id')." then 1 else 0 end) as '". mssql_result($result, $i, 'Description') ."'\n";
        $b[] = "<td>".mssql_result($result, $i, 'Description')."</td>";
        $c[] = "<td>.mssql_result($"."resulta, $"."ia, '".mssql_result($result, $i, 'Description')."').</td>";
}
 
 
$querya = "SELECT l.Location , " . implode(", ", $a) .
       " FROM Inventory I
         JOIN conf_Company C
         ON I.ID_Company=c.ID
         JOIN conf_Location l
         ON I.ID_Location=L.ID
         JOIN conf_Inventory_Model M
         ON I.ID_Model=M.ID
         WHERE C.Company='$usernew'
         GROUP BY L.Location";
 
 
 
 
$resulta = mssql_query($querya);
 
for($ia = 0; $ia < mssql_num_rows($resulta); $ia++)
{
        $l[] = "<tr><td>".mssql_result($resulta, $ia, 'location')."</td>". implode($c) .   " </tr>";
 
 
 
}
ho '<br><br>';
echo $result;
 
echo '<br><br>';
echo '<Table>';
echo '<tr>';
echo '<td>Location</td>';
	echo implode($b);
echo '</tr>';
echo '<tr>';
	echo implode($l);
echo '</tr>';
echo '</Table>';

Open in new window

line 51
needs ec added don't how I missed that

echo '<br><br>';
Any ideas?
I have also increased the points
A couple of questions:

Why is $usernew never set?
What are you doing with the generated number?

Extra protip: use self-describing variables!!
It makes it much more readible.
Anyways try running the code below (and read my comments!!!)



<?php
//DS: Where is $usernew set?
$query = "select Distinct m.ID, m.Description
from conf_Inventory_Model m
      join Inventory i
        on m.ID=i.ID_Model
      join conf_Company c
        on i.ID_Company=c.ID
where c.Company='$usernew' and
      substring(m.Description, 1, 2)= 'MC'"; 
 
 
$result = mssql_query($query);
 
$a = array();
$b = array();
$c = array();
$l = array();
 
mt_srand((double)microtime()*1000000); //DS: not needed?
for($i = 0; $i < mssql_num_rows($result); $i++)
{
 
        $a[] = "Sum(Case when i.ID_Model=".mssql_result($result, $i, 'id')." then 1 else 0 end) as '". mssql_result($result, $i, 'Description') ."'\n";
        $b[] = "<td>".mssql_result($result, $i, 'Description')."</td>"; 
  /*
  DS:
  Where is $ia set? or do you mean the values of $a?
  then it should be: $a[$i]
  */
        //$c[] = "<td>".mssql_result($resulta, $ia, mssql_result($result, $i, 'Description'))."</td>";
  $c[] = "<td>".mssql_result($resulta, $a[$i], mssql_result($result, $i, 'Description'))."</td>"; //DS: you mean something like this?
  
}
 
 
$querya = "SELECT l.Location , " . implode(", ", $a) .
       " FROM Inventory I
         JOIN conf_Company C
         ON I.ID_Company=c.ID
         JOIN conf_Location l
         ON I.ID_Location=L.ID
         JOIN conf_Inventory_Model M
         ON I.ID_Model=M.ID
         WHERE C.Company='$usernew'
         GROUP BY L.Location";
 
 
 
 
$resulta = mssql_query($querya);
 
for($ia = 0; $ia < mssql_num_rows($resulta); $ia++)
{
        $l[] = "<tr><td>".mssql_result($resulta, $ia, 'location')."</td>". implode($c) .   " </tr>";
 
 
 
}
echo '<br><br>';
echo $result;
 
echo '<br><br>';
echo '<Table>';
echo '<tr>';
echo '<td>Location</td>';
 echo implode($b);
echo '</tr>';
echo '<tr>';
 echo implode($l);
echo '</tr>';
echo '</Table>';
?>

Open in new window

new user is set above it is the login

ia -> this where set in the for and thats what cousing my issue.

it is set below
$ia is indeed set at the bottom... but you are using it above this!

I can safely assure you that the first time $ia is called for, it's not there: PHP works from top -> bottom
I know but I can not figure out how to make this code work
no matter how I arrage it I can not get it to work
I have attached another code to try and get this resolved.

All I need to do is make this part automatic
            <td>".mssql_result($resulta, $ia, $c[0])."</td>
            <td>".mssql_result($resulta, $ia, $c[1])."</td>
            <td>".mssql_result($resulta, $ia, $c[2])."</td>
            <td>".mssql_result($resulta, $ia, $c[3])."</td>
            <td>".mssql_result($resulta, $ia, $c[4])."</td></tr>";

With some while statment or something

$query = "select Distinct m.ID, m.Description
from conf_Inventory_Model m
      join Inventory i
        on m.ID=i.ID_Model
      join conf_Company c
        on i.ID_Company=c.ID
where c.Company='$usernew' and
      substring(m.Description, 1, 2)= 'MC'";
 
 
$result = mssql_query($query);
 
$a = array();
$b = array();
$c = array();
$l = array();
 
mt_srand((double)microtime()*1000000);
for($i = 0; $i < mssql_num_rows($result); $i++)
{
 
        $a[] = "Sum(Case when i.ID_Model=".mssql_result($result, $i, 'id')." then 1 else 0 end) as '". mssql_result($result, $i, 'Description') ."'\n";
        $b[] = "<td>".mssql_result($result, $i, 'Description')."</td>";
        $c[] = mssql_result($result, $i, 'Description');
 
}
 
 
$querya = "SELECT l.Location , " . implode(", ", $a) .
       " FROM Inventory I
         JOIN conf_Company C
         ON I.ID_Company=c.ID
         JOIN conf_Location l
         ON I.ID_Location=L.ID
         JOIN conf_Inventory_Model M
         ON I.ID_Model=M.ID
         WHERE C.Company='$usernew'
         GROUP BY L.Location";
 
 
 
 
$resulta = mssql_query($querya);
 
 
for($ia = 0; $ia < mssql_num_rows($resulta); $ia++)
{
        $l[] = "<tr><td>".mssql_result($resulta, $ia, 'location')."</td>
		<td>".mssql_result($resulta, $ia, $c[0])."</td>
		<td>".mssql_result($resulta, $ia, $c[1])."</td>
		<td>".mssql_result($resulta, $ia, $c[2])."</td>
		<td>".mssql_result($resulta, $ia, $c[3])."</td>
		<td>".mssql_result($resulta, $ia, $c[4])."</td></tr>";
 
 
}
 
 
 
echo '<br><br>';
echo $i;
echo '<br><br>';
echo '<Table>';
echo '<tr>';
echo '<td>Location</td>';
	echo implode($b);
echo '</tr>';
echo '<tr>';
	echo implode($l);
echo '</tr>';
echo '</Table>';

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of deefjuh
deefjuh

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You use an array (by setting it to $l[] --> the brackets cast it to an array), and implode it afterwards (wihout actually using it as an array).
When you want to set it as a string, just use the . as glue to get the pieces of text together.

By this I mean:
Assign variables without a []. (look below).
 
www.php.net :love it, embrace it, use it! And look at the usercomments below the function explanation, they contain usefull snippets.
 
 

$string = 'my text'; 
$string .= ' is now bigger';
 
echo $string; // will print 'my text is now bigger'
 
while you use:
$string[] = 'my text';
$string[] = ' is now bigger';
echo implode($string);
 
This is not an error, but a bad coding practice.

Open in new window

no matter what I do I get

Warning: mssql_result() [function.mssql-result]: Bad row offset (139)
I see I made an error at my foreach-loop. I misinterpreted the wanted result for the rows.
Anyways, if you gave some test data (the create query for the data and some rows), I would get it up and running in notime.
$query = "select Distinct m.ID, m.Description
from conf_Inventory_Model m
      join Inventory i
        on m.ID=i.ID_Model
      join conf_Company c
        on i.ID_Company=c.ID
where c.Company='$usernew' and
      substring(m.Description, 1, 2)= 'MC'";
 
 
$result = mssql_query($query);
 
$a = array();
$b = array();
$c = array();


mt_srand((double)microtime()*1000000);
for($i = 0; $i < mssql_num_rows($result); $i++)
{
 
        $a[] = "Sum(Case when i.ID_Model=".mssql_result($result, $i, 'id')." then 1 else 0 end) as '". mssql_result($result, $i, 'Description') ."'\n";
        $b[] = "<td>".mssql_result($result, $i, 'Description')."</td>";
        $c[] =  mssql_result($result, $i, 'Description');

}
 
$querya = "SELECT l.Location , " . implode(", ", $a) .
       " FROM Inventory I
         JOIN conf_Company C
         ON I.ID_Company=c.ID
         JOIN conf_Location l
         ON I.ID_Location=L.ID
         JOIN conf_Inventory_Model M
         ON I.ID_Model=M.ID
         WHERE C.Company='$usernew'
         GROUP BY L.Location";
 
$resulta = mssql_query($querya);
$ia = mssql_num_rows($resulta);


$ia = $ia -1;
$counter = 0;
$m="";

while ( $counter <= $ia ) {

$m .= '<tr>';
$m .= '<td>'.mssql_result($resulta, $counter, 'location').'</td>';
foreach($c as $key){
    $m .= "<td>".mssql_result($resulta, $counter, $key)."</td>";
}
$m .= '</tr>';
$counter = $counter +1;
}
echo '<br><br>';
echo '<Table>';
echo '<tr>';
echo '<td>Location</td>';
echo implode($b);
echo '</tr>';
echo  $m;
echo '</Table>';

//close the connection
mssql_close($dbhandle);