Jump to content

Getting a basic loop


oceanv5

Recommended Posts

Hi,

 

I have the following code:

 

$seg1 = mysql_query("SELECT * FROM area WHERE id LIKE 'SE1%' ")  or die(mysql_error()); 

while($row = mysql_fetch_array( $seg1 )) {

  $segment1 .=  $row['base'];

}

 

 

To fetch "base" depending on a wildcard "id"

 

I want to create a loop where each of the following changes

 

right now it's seg1 --> SE1%

...

seg2 --> SE2%

seg3 --> SE3%

and so on...

 

How would I go about creating a loop like this?

Link to comment
Share on other sites

Just to clarify-- the following code:

$seg1 = mysql_query("SELECT * FROM area WHERE id LIKE 'SE1%' ")  or die(mysql_error()); 
while($row = mysql_fetch_array( $seg1 )) {
   $segment1 .=  $row['base'];
}

 

To fetch "base" depending on a wildcard "id"

 

I want to create a loop where each of the following changes

 

right now it's seg1 --> SE1%

...

seg2 --> SE2%

seg3 --> SE3%

and so on...

 

How would I go about creating a loop like this?

Link to comment
Share on other sites

How about doing something like this:

<?php
$segment = array();
$q = "SELECT base, id FROM area WHERE id LIKE 'SE%'";
$rs = mysql_query$($q) or die("Problem with the query: $q<br>" . mysql_error());
while ($row = mysql_fetch_assoc($rs)) {
    $n = substr($row['id'],2,1);
    if (!is_array($segment[$n])) $segment[$n] = array();
    $segment[$n][] = $row['base'];
}
echo '<pre>' . print_r($segment,true) . '</pre>'; //dump what's in the array
?>

 

Ken

Link to comment
Share on other sites

sorry, the code I have:

$seg1 = mysql_query("SELECT * FROM area WHERE id LIKE 'SE1%' ")  or die(mysql_error()); 
while($row = mysql_fetch_array( $seg1 )) {
   $segment1 .=  $row['base'];
}


$seg2 = mysql_query("SELECT * FROM area WHERE id LIKE 'SE2%' ")  or die(mysql_error()); 
while($row = mysql_fetch_array( $seg2 )) {
   $segment2 .=  $row['base'];
}

...


$seg55 = mysql_query("SELECT * FROM area WHERE id LIKE 'SE55%' ")  or die(mysql_error()); 
while($row = mysql_fetch_array( $seg55 )) {
   $segment55 .=  $row['base'];
}

 

I'm trying to construct an array so I don't have to keep typing over and over.

 

The above statements grab a string of data constructed in a mysql table "area"

 

id | base
---------
SE1_1, a
SE1_2, g
SE1_3, g
...
SE55_35,t

 

 

These are displayed via a php query inside javascript code, where I can alternate multiple elements in the page depending on whether the user clicks "segment 1", "segment 2" and so on...

 

<?php echo $segment1 ?>

 

This reproduces something akin:

agctgggctgaactgatgccgagatcgaa... etc. in my page.

 

---

 

I used your code exactly as shown, and modified the echo statement, but then the page did not produce anything.

Link to comment
Share on other sites

The user only click one segment or ?

its better if you provide the form to help us understand your intention.

its not wise to do select query for 55 times if it can just be combined into a single query.

And then also please explain what is your intended output

 

Link to comment
Share on other sites

The elements are as follows:

 

in the HTML page user clicks a segment in each section

 

part one (segment 1) (segment 2)

part two (segment 3) (segment 4) (segment 5) (segment 6)

...

part seventeen (segment 33) (segment 34) (segment 35)...

 

this produces text fetched from the database, associated with each segment.

 

I'm doing this via javascript and it works fine. In the innerHTML of my script I have a <php echo...> statement.

 

Again this works fine.

 

However, in the main function where I'm querying the actual database, I am going to have hundreds of segments being queried where as I described in my previous post, only three elements are changing -- the query name, the MYSQL segment id, and the variable for the echo. I was wondering if there is a way to crunch everything into one and then use arrays to fill those three variables. It sounds like it should be simple but I can't figure it out.  :-\

Link to comment
Share on other sites

Not sure what you mean, there is no form...

 

In the HTML page

 

User clicks

			<a id="seg1"><img src="images/click1.gif"></a>
		<a id="seg2"><img src="images/click2.gif"></a>
		<a id="seg3-1"><img src="images/click3-1.gif"></a>
		<a id="seg3-2"><img src="images/click3-2.gif"></a>

 

Elsewhere on the page the following changes into TEXT fetched from the query...

<span class="" id="seg1text">--</span>
<span class="" id="seg2text">--</span>
<span class="" id="seg3text">--</span>

 

Other elements and classes change too, but too complicated to mention here and has nothing to do with the PHP.

 

The javascript

window.onload = pageLoad; // global code
function pageLoad() {

  var seg1 = document.getElementById("seg1text");
  seg1.onclick = seg1Click;
  var seg2 = document.getElementById("seg2text");
  seg2.onclick = seg2Click; 
  var seg3-1 = document.getElementById("seg3text");
  seg3-1.onclick = seg3-1Click; 
  var seg3-2 = document.getElementById("seg3text");
  seg3-2.onclick = seg3-2Click; 

}

function seg1Click() {   
  document.getElementById('seg1text').innerHTML='<?php echo $segment1 ?>';
  
}
function seg2Click() {  
  document.getElementById('seg2text').innerHTML='<?php echo $segment2 ?>';
  
}
function seg3-1lick() {   
  document.getElementById('seg3text').innerHTML='<?php echo $segment3_1 ?>';
    
}

function seg3-2Click() {   
  document.getElementById('seg3text').innerHTML='<?php echo $segment3_2 ?>';
    
}

 

The PHP

$seg1 = mysql_query("SELECT * FROM dna_dmelanogaster WHERE id LIKE 'SE1%' ")  or die(mysql_error());  
while($row = mysql_fetch_array( $seg1 )) {
   $segment1 .=  $row['base'];
}

$seg2 = mysql_query("SELECT * FROM dna_dmelanogaster WHERE id LIKE 'SE2%' ")  or die(mysql_error());  
while($row = mysql_fetch_array( $seg2 )) {
   $segment2 .=  $row['base'];
}

$seg3_1 = mysql_query("SELECT * FROM dna_dmelanogaster WHERE id LIKE 'SE3_1%' ")  or die(mysql_error());  
while($row = mysql_fetch_array( $seg3 )) {
   $segment3_1 .=  $row['base'];
}

$seg3_2 = mysql_query("SELECT * FROM dna_dmelanogaster WHERE id LIKE 'SE3_2%' ")  or die(mysql_error());  
while($row = mysql_fetch_array( $seg3 )) {
   $segment3_2 .=  $row['base'];
}

 

The MYSQL

id / base
---------
SE1_1,a
SE1_2,a
SE1_3,g
...
SE3_2_1,c
SE3_2_2,g
SE3_2_3,c
...

 

Anyway, all of this works fine. Except the PHP file is massive and just repeats the same function over and over.

Link to comment
Share on other sites

Yes, thats what I'm asking. The HTML page.

I suppose this would do.

$q = "SELECT base, id FROM area WHERE id LIKE 'SE%'";
$rs = mysql_query($q) or die("Problem with the query: $q<br>" . mysql_error());
while ($row = mysql_fetch_assoc($rs)) {
$temparr = explode("_",$row['id']);
$tempseg = "segment".(substr($temparr[0],2));
$$tempseg .= $row['base'];
}

 

Link to comment
Share on other sites

thanks so much... works fantastically for situations where seg1 --> seg1%, seg2 --> seg2%... etc.

 

but not so for seg4_1 --> seg4_1% etc. should I add a new statement for that? do I need to change all the variables?

Link to comment
Share on other sites

The following will (untested) generate all your $segmentx variables from the corresponding SEx data -

 

$query = "SELECT base, id FROM area";
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result)){
   $loc = strrpos($row['id'],"_"); // find the last _
   $seg = substr($row['id'],0,$loc); // get the string upto the last _
   $seg = str_replace("SE","segment",$seg); // convert SE to segment
   if(!isset($$seg)){$$seg = '';} // create empty variable when it does not exist
   $$seg .= $row['base']; // concatenate data
}

 

If you study this, you will also see that you can dynamically generate all the rest of the html and javascript on the page using similar code (no hand typing/hardcoding is needed.) Edit: Actually, you could generate it inside of the if(!isset($$seg)){$$seg = '';} statement when each new id is detected.

Link to comment
Share on other sites

The following would give you something to consider on how you could generate all the html and javascript dynamically -

 

$content_a = ""; // HTML href links
$content_b = ""; // HTML spans
$content_c = ""; // javascript
$content_d = ""; // javascript
$arr = array(); // array of the data (and keys)
$query = "SELECT base, id FROM area";
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result)){
   $loc = strrpos($row['id'],"_");
   $seg = substr($row['id'],0,$loc);
   $seg = str_replace("SE","",$seg); // the number only
if(!isset($arr[$seg])){
	$hseg = str_replace("_","-",$seg); // change _ to - in order to match existing html/javascript
	$arr[$hseg] = ''; // create variable when it does not exist
	$content_a .= "<a id=\"seg{$hseg}\"><img src=\"images/click{$hseg}.gif\"></a>\n";
	$content_b .= "<span class=\"\" id=\"seg{$hseg}text\">--</span>\n";
	$content_c .= "\tvar seg{$hseg} = document.getElementById(\"seg{$hseg}text\");\n\tseg{$hseg}.onclick = seg{$hseg}Click;\n";
}
   $arr[$hseg] .= $row['base'];
}

// produce the data specific javascript 
foreach($arr as $key => $value){
$content_d .= "function seg{$key}Click(){document.getElementById('seg1text').innerHTML='$value';}\n";
}
echo $content_a; // output the HTML links
echo $content_b; // output the span section
?>
<script type="text/javascript">
window.onload = pageLoad; // global code
function pageLoad() {
<?php echo $content_c; ?>
}
<?php echo $content_d; ?>
</script>

Link to comment
Share on other sites

If you are referring to the last code I posted. It is incomplete and untested because it was only to illustrate how you might accomplish something, based on the limited information you did post, and was not 'actual' working code (at a minimum, the reference to !isset($arr[$seg]) needs to be changed to $hseg and the line setting $hseg needs to be moved to before the if() statement.)

Link to comment
Share on other sites

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.