Jump to content


Photo

Hopefully simple solution


  • Please log in to reply
3 replies to this topic

#1 sylunt1

sylunt1
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 15 May 2006 - 01:33 AM

Hello...

I am trying to do some basic js stuff and need a little help. I have the following code;

<script LANGUAGE="JavaScript">
<!-- HIDE FROM OTHER BROWSERS

var side = new Array("Oki 590-88", "Oki 590-79", "Oki 473C");
var top = new Array("Manufacturer", "Description", "OEM Price", "HDC Price");
                            

var manu = new Array("IBM", "IBM", "Okidata", "Okidata");
var part = new Array(976, 726, 590, 473);
var fits = new Array("2380, 2381, 2390, 2391", "3262, 5262", "590, 591", 120);
var price = new Array("6.00", "6.25", "16.50", "10.75");

document.write("<table border=1 width=95% cellspacing=0 height=1 cellpadding=1><tr><th>Manufacturer</th><th>Part</th><th>Fits Model</th><th>Price</th></tr>");

for (i=0; i<manu.length; i++){

   
document.write("<tr><td>", manu[i], "</td>",
"<td>", part[i], "</td>",
"<td>", fits[i], "</td>", 
"<td>", price[i], "</td></tr>") 


}
document.write("</table>");

// STOP HIDING FROM OTHER BROWSERS -->
</SCRIPT>

It does what it is supposed to do, but what I want is this:
on the left column - if the item repeats I only want the word to show up one time - like this...

item 1 product info price
[empty] product info price
item 2 product info price
[empty] product info price

How can I make it do that?


Thanks,
Chris

#2 _will

_will
  • Members
  • PipPip
  • Member
  • 25 posts

Posted 15 May 2006 - 04:04 AM

try this in your for loop...

var last = "";
var display = "";
for (i=0; i<manu.length; i++){
if (last == manu[i]) {
  display = manu[i];
  last = manu[i];
}
else {
  display = "";
}
  
document.write("<tr><td>", display, "</td>",
"<td>", part[i], "</td>",
"<td>", fits[i], "</td>",
"<td>", price[i], "</td></tr>")


}

All I added was a variable to track the last product shown and then an if statement to see if the next product is the same as the last product shown. If it is, then no product name is printed.

#3 sylunt1

sylunt1
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 15 May 2006 - 04:25 AM

Almost... now none of the manufacturers show up... its all blank...

#4 emehrkay

emehrkay
  • Staff Alumni
  • Advanced Member
  • 1,214 posts

Posted 15 May 2006 - 04:53 AM

yes the solution is simple (i hope it works for ya :)

if(i !=0 && part[i-1] != part[i]){
display part[i]
}else{
display nothing
}




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users