Jump to content

Recommended Posts

Hi Guys

 

how do i streamline this code and generally make it better?

 

<?php

$query2 = "SELECT id, col_1, col_2, col_3, col_4, col_5, col_6, col_7, col_8, col_9, col_10 FROM prices WHERE product = '".$product_name."' AND col_1 !='' or col_2 !='' or col_3 !='' or col_4 !='' or col_5 !='' or col_6 !='' or col_7 !='' or col_8 !='' or col_9 !='' or col_10 !=''  ORDER BY id";

$res2 =mysql_query($query2) or die(mysql_error());

$num_rows = mysql_num_rows($res2);

$i=1;
while($row = mysql_fetch_array($res2)){

while($i<=$num_rows)
  {
  echo "<li><a rel=\"nofollow\" class=\"opt\" href=\"#\">".$row[$i]."<span>".$row[$i]."</span></a></li>";
  $i++;
  }
}
?> 

Link to comment
https://forums.phpfreaks.com/topic/253670-how-do-i-streamline-this-code/
Share on other sites

Firstly there is an logical error in Your query. all "OR"cols should be grouped in parenthesis.

 

SELECT id, col_1, col_2, col_3, col_4, col_5, col_6, col_7, col_8, col_9, col_10

FROM prices

WHERE product = '".$product_name."' AND (col_1 !='' or col_2 !='' or col_3 !='' or col_4 !='' or col_5 !='' or col_6 !='' or col_7 !='' or col_8 !='' or col_9 !='' or col_10 !='' )

ORDER BY id

 

You should change SELECT id, col_1, col_2, col_3, col_4, col_5, col_6, col_7, col_8, col_9, col_10 to SELECT *

 

without the brackets it pulls the correct result. With the brakets it doesnt pull the right number.

 

I was just hoping there was a way to write this where i didn't have to write out the col_1 !='' or col_2 !='' or col_3 !='' or col_4 !='' or col_5 !='' or col_6 !='' or col_7.

Was thinking of a loop col_$i !=''

And a way of finding out how many cells was in the table i.e col_1 to col_10?

I putting the (correct) brackets into your (wrong) query produces what you call the "wrong" results, you might not be understanding what it's trying to do.  Right now, your query will return rows if:

product = '".$product_name."' AND col_1 !=''

OR

col2 != ''

OR 

col3 != ''

etc.

That doesn't appear to be what you want.

 

Also, Thorpe is absolutely right about your design.  Either:

A)  These fields all should be named properly and you just didn't bother for some reason.  You should name the fields in this case.

B)  These fields all hold the same kind of data and you're adding a new numbered column whenever you add more data.  You should break these columns out to another table in this case (which would have the side effect of making your query easy)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.