oceanv5 Posted May 27, 2010 Share Posted May 27, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/203140-getting-a-basic-loop/ Share on other sites More sharing options...
oceanv5 Posted May 28, 2010 Author Share Posted May 28, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/203140-getting-a-basic-loop/#findComment-1064674 Share on other sites More sharing options...
kenrbnsn Posted May 28, 2010 Share Posted May 28, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/203140-getting-a-basic-loop/#findComment-1064689 Share on other sites More sharing options...
oceanv5 Posted May 28, 2010 Author Share Posted May 28, 2010 Hi, thanks so much... it doesn't appear to be working. Should I write the array? How does it account for the fact that SE% keeps changing according to segment#? Quote Link to comment https://forums.phpfreaks.com/topic/203140-getting-a-basic-loop/#findComment-1064762 Share on other sites More sharing options...
kenrbnsn Posted May 28, 2010 Share Posted May 28, 2010 Clarify "it doesn't appear to be working" Without seeing more of your code or knowing exactly what you're trying to do... Ken Quote Link to comment https://forums.phpfreaks.com/topic/203140-getting-a-basic-loop/#findComment-1064765 Share on other sites More sharing options...
oceanv5 Posted May 28, 2010 Author Share Posted May 28, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/203140-getting-a-basic-loop/#findComment-1064779 Share on other sites More sharing options...
oceanv5 Posted May 31, 2010 Author Share Posted May 31, 2010 Any thoughts? Quote Link to comment https://forums.phpfreaks.com/topic/203140-getting-a-basic-loop/#findComment-1065828 Share on other sites More sharing options...
riwan Posted May 31, 2010 Share Posted May 31, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/203140-getting-a-basic-loop/#findComment-1065830 Share on other sites More sharing options...
oceanv5 Posted May 31, 2010 Author Share Posted May 31, 2010 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. :-\ Quote Link to comment https://forums.phpfreaks.com/topic/203140-getting-a-basic-loop/#findComment-1065837 Share on other sites More sharing options...
riwan Posted May 31, 2010 Share Posted May 31, 2010 you should still attach your form coding Quote Link to comment https://forums.phpfreaks.com/topic/203140-getting-a-basic-loop/#findComment-1065842 Share on other sites More sharing options...
oceanv5 Posted May 31, 2010 Author Share Posted May 31, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/203140-getting-a-basic-loop/#findComment-1065847 Share on other sites More sharing options...
riwan Posted May 31, 2010 Share Posted May 31, 2010 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']; } Quote Link to comment https://forums.phpfreaks.com/topic/203140-getting-a-basic-loop/#findComment-1065850 Share on other sites More sharing options...
oceanv5 Posted May 31, 2010 Author Share Posted May 31, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/203140-getting-a-basic-loop/#findComment-1065892 Share on other sites More sharing options...
oceanv5 Posted May 31, 2010 Author Share Posted May 31, 2010 sorry I should clarify, it works to fetch these SE1_1,a SE1_2,a SE1_3,g but not SE3_2_1,c SE3_2_2,g SE3_2_3,c Quote Link to comment https://forums.phpfreaks.com/topic/203140-getting-a-basic-loop/#findComment-1065893 Share on other sites More sharing options...
PFMaBiSmAd Posted May 31, 2010 Share Posted May 31, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/203140-getting-a-basic-loop/#findComment-1065902 Share on other sites More sharing options...
riwan Posted May 31, 2010 Share Posted May 31, 2010 but not SE3_2_1,c SE3_2_2,g SE3_2_3,c didn't see your html code for this so not testing for it. anyway the above code should work, post again if you still have some trouble Quote Link to comment https://forums.phpfreaks.com/topic/203140-getting-a-basic-loop/#findComment-1065904 Share on other sites More sharing options...
PFMaBiSmAd Posted May 31, 2010 Share Posted May 31, 2010 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> Quote Link to comment https://forums.phpfreaks.com/topic/203140-getting-a-basic-loop/#findComment-1065911 Share on other sites More sharing options...
oceanv5 Posted June 1, 2010 Author Share Posted June 1, 2010 Hey thanks... It looks safe (although not sure where the wildcard function is) it doesn't reproduce anything though, I've tried to mess around with it. :-\ Quote Link to comment https://forums.phpfreaks.com/topic/203140-getting-a-basic-loop/#findComment-1066159 Share on other sites More sharing options...
PFMaBiSmAd Posted June 1, 2010 Share Posted June 1, 2010 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.) Quote Link to comment https://forums.phpfreaks.com/topic/203140-getting-a-basic-loop/#findComment-1066215 Share on other sites More sharing options...
oceanv5 Posted June 1, 2010 Author Share Posted June 1, 2010 sorry, was referring to the previous page (didn't even see there was a page 2!) but thank you, the stuff above looks grand... will play around with it. Quote Link to comment https://forums.phpfreaks.com/topic/203140-getting-a-basic-loop/#findComment-1066238 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.