Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help make code look same in IE and Firefox please 1

Status
Not open for further replies.
May 13, 2005
56
US
Hi there, I have a piece of code that searches for a name in all the fields of a table, if it finds that name, I then display some of the contents of that row that it found it in.

Now with the loop, the code reads each row of the db and displays the data if the if statement is true. What is happening is, when viewing in IE, it looks like I would expect it to, but when viewing in Firefox, there is a small space that is put on the page for each time the if statement returns false.. I want to remove this space..

Here is a snippit of the code
Code:
// Start to process game data
mysql_connect('localhost',$username,$password);
mysql_select_db($database) or die("Unable to select database");
$query=("SELECT * FROM games");
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();

$i=0;
while ($i < $num) {

$one=mysql_result($result,$i,"1st");
$two=mysql_result($result,$i,"2nd");
$three=mysql_result($result,$i,"3rd");
$four=mysql_result($result,$i,"4th");
$five=mysql_result($result,$i,"5th");

if ($one == $name) {
    print"all items in row if the name is in the one field";
}
if ($two == $name) {
    print"all items in row if the name is in the two field";
}
if ($three == $name) {
    print"all items in row if the name is in the three field";
}
if ($four == $name) {
    print"all items in row if the name is in the four field";
}
if ($five == $name) {
    print"all items in row if the name is in the five field";
}
$i++;
}

Now, Im sure there is a better way to make this work, I just havent figured it out, if you have any suggestions on how to improve my code, that would be appriciated also.
I tried to set up something like a fuction to do all the if statements but within the loop it gave me troubles so I just types them out as you see.

Thanks..

Mike
 
For imporvement of the overall structure of the mysql stuff read faq434-3850

Firefox is much closer to web standards than IE. In general, Mozilla based browsers do not fare well with empty table cells. When nothing is printed you should output at least a non-breaking-space &nbsp; or the table cells will collapse.
 
Thanks for the input guys.. I actually resolved the issue as it was related to my syntax with some code I left out..
I had the <table> tags inside the loop, once I moved those and put the <td> tags in each if statement, it was cleared up..

Now I have a couple of questions on your comments..

DRJ478,

Thanks for the link, I have a question about your comment for placing the &nbsp; if there is no output. Wouldn't that increase my spaces?

Also, you had mention this to me in another thread "Try to learn how to think abstract. How can you abstract the procedure you have and make it into a function that receives input arguments and then processes the data. The processing is always the same, the arguments change."

That was great advice and it actually took me down from 1000 line of code to about 150... Now is there a way to apply that logic to my if statements here? I have tried, but the loops are throwing it off for me a little.

DaButcher,

Are you saying that would be a good idea in general, or specifically to my code examples.

Thanks guys, I appriciate the input..

Mike
 
I dont know what your code does, but I guess you could use arrays with keys and currents and then simply replace.

Almost like a smiley-script, where you replace :), :(, etc.

Also, using switch might save you some lines of code, and it would look a tad nicer.

As for the XHTML, that was a tip in general. (for making pages look as identical as possible, on different browsers).

It's quite easy actually, so I think it's good practice to always write xhtml 1.0

Olav Alexander Mjelde
Admin & Webmaster
 
Cool, thanks for your reply, I will look into your suggestions..

I need to clean that up, this should help give me an idea where to get started..

I will read up on the XHTML 1.0 also..
 
First step for abstraction:
Can you define your problem in words?
I want to get the first five ...? Please explain.
 
Ok, here goes..

The problem:
Here is a little diagram to help me explain it:
The numbers on the side will signify rows and the letters across the top are fields, a,b,c,d and e are data.

Code:
  A B C D E
1 c d e a b
2 d a b c e 
3 a c e b d
4 a c d e b
5 e d c a b
a,b,c,d and e will always be in the same row together.

Now onto my problem,
If 'a' exists in field A, I would like to display the contents of the row that it was in. for example -
Code:
$value='a';
$value2='b'; // etc...

if ($value == 'a'){ // This would find 'a' in row 3
  $A = a;
  $B = c;
  $C = e;
  $D = b;
  $E = d;
}
then display those contents in a table, and do this for each time it finds it in a field.
now if I have to do this 20+ times, you can see why I want to slim it down..

 
So, recap:
1. There are records which have 5 variables in any conceivable order.
2. You want to go through these records ordered by the first column which is compared to a static value which is supplied.

Some thoughts:
A single MySQL statement to retrieve all the rows might not be the way to go. Your problem has to do with restricted sets of records, so retrieveing more than necessary and comparing them after is more time consuming and difficult than getting a record set which only contains pertinent data.
A table that is indexed by column (you call it field) "A" retrieves records easily and very quickly given a SQL statement like:
Code:
$SQL = "SELECT * FROM table WHERE `A` = 'a'";

Here we can begin the abstraction.
Write a little function that receives the value for the condition and prints out all results in an HTML table.

Suggestion:
Code:
function showRecords($condition){
   $SQL = "SELECT * FROM table WHERE `A` = '$condition'";
   $result = mysql_query($SQL) OR die(mysql_error());
   while ($row = mysql_fetch_assoc($result)){
      # HTML printing stuff here
   }
}

## Main code puts the values searched for in an array:
$mySearch[] = 'a';
$mySearch[] = 'b';
# etc.
# iterate
foreach ($mySearch as $condition){
    showRecord($condition);
}

Now we are really much more abstract.
What we switched was: instead of using one global SQL that gets all rows we issue multiple precise queries and we save the comparing of the resulting records. MySQL is much faster and better doing that.
The higher the specifity of the result set, the easier it is to process.
 
Nice, I was stuck with my thinking and didnt see it that way before...

Thank you for your help, I will try that out, i'm sure it will help..

Thanks again, I really appriciate it..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top