Solved

putting array into array

Posted on 2009-07-08
14
227 Views
Last Modified: 2013-12-13
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.
0
Comment
Question by:adamn123
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
14 Comments
 
LVL 1

Expert Comment

by:deefjuh
ID: 24804058
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
 

Author Comment

by:adamn123
ID: 24804174
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
 

Author Comment

by:adamn123
ID: 24804751
line 51
needs ec added don't how I missed that

echo '<br><br>';
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:adamn123
ID: 24805245
Any ideas?
I have also increased the points
0
 
LVL 1

Expert Comment

by:deefjuh
ID: 24805516
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
 

Author Comment

by:adamn123
ID: 24805754
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
 
LVL 1

Expert Comment

by:deefjuh
ID: 24805966
$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
 

Author Comment

by:adamn123
ID: 24805987
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
 

Author Comment

by:adamn123
ID: 24806418
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
 
LVL 1

Accepted Solution

by:
deefjuh earned 250 total points
ID: 24806653
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
 
LVL 1

Expert Comment

by:deefjuh
ID: 24806704
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
 

Author Comment

by:adamn123
ID: 24807151
no matter what I do I get

Warning: mssql_result() [function.mssql-result]: Bad row offset (139)
0
 
LVL 1

Expert Comment

by:deefjuh
ID: 24807748
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
 

Author Comment

by:adamn123
ID: 24807765
$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

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question