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.
adamn123Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

deefjuhCommented:
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

0
adamn123Author Commented:
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

0
adamn123Author Commented:
line 51
needs ec added don't how I missed that

echo '<br><br>';
0
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

adamn123Author Commented:
Any ideas?
I have also increased the points
0
deefjuhCommented:
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

0
adamn123Author Commented:
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
0
deefjuhCommented:
$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
0
adamn123Author Commented:
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
0
adamn123Author Commented:
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

0
deefjuhCommented:
You would do that with a foreach-loop (like I attached).

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.


replace
echo implode($l);

with
echo $l;
$l = '<tr>';
foreach($c as $key => $value){
    $l .= "<td>".mssql_result($resulta, $ia, $value)."</td>";
}
$l = '</tr>';
 
/*
replace below
echo implode($l);
 
with
echo $l;
*/

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
deefjuhCommented:
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

0
adamn123Author Commented:
no matter what I do I get

Warning: mssql_result() [function.mssql-result]: Bad row offset (139)
0
deefjuhCommented:
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.
0
adamn123Author Commented:
$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);
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.