can any one give me some link on recursion so that I can practice more?
thanks for replying James. I always loose the stack sequence.. I don't really know how to improve this
the code is here:
import java.util.Scanner;
class Anagram {
static int size;
static int count;
static char[] arrChar = new char[100];
static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
System.out.print("Enter a word: "); // get word
String input = in.next();
size = input.length(); // find its size
count = 0;
for (int j = 0; j < size; j++) // put it in array
{
arrChar[j] = input.charAt(j);
}
doAnagram(size); // anagram it
} // end main()
//-----------------------------------------------------------
public static void doAnagram(int newSize) {
if (newSize == 1) // if too small,
return; // go no further
for (int j = 0; j < newSize; j++) // for each position,
{
doAnagram(newSize - 1); // anagram remaining
if (newSize == 2) // if innermost,
{
displayWord(); // display it
}
rotate(newSize); // rotate word
}
}
//-----------------------------------------------------------
// rotate left all chars from position to end
public static void rotate(int newSize) {
int j;
int position = size - newSize;
char temp = arrChar[position]; // save first letter
for (j = position + 1; j < size; j++) // shift others left
{
arrChar[j - 1] = arrChar[j];
}
arrChar[j - 1] = temp; // put first on right
}
//-----------------------------------------------------------
public static void displayWord() {
System.out.print(++count + " ");
for (int j = 0; j < size; j++) {
System.out.print(arrChar[j]);
}
System.out.print(" ");
if (count % 6 == 0) {
System.out.println("");
}
}
} // end class Anagram
My question is why rotate (4) not trigger first. It triggers 2 then 3 then 4
Hi, before posting this thread I tried to find solution inside daniweb but was not successful. I found one but it was so specific for one's own homework problem. I always get lost when I do recursion. I read books but most of them start with Fibonacci or Hannoi problem then Good luck! No books talks little details about how recusion in a for loop or while loop works. I will be really greatful if you guys give some time to give me some very simple example about recusion in a for loop works. Please comment code or write down few words.
Thanks.
Hello Experts,
I am getting unwanted result.Please help!
Here is the function:
function ArrayResults($result){
while ($row = mysql_fetch_assoc($result)){
//echo $row["first_name"]; //tested :works!!
$this->arr2[] = $row;
}
return $this->arr2;
}
I called the function and used the print_r to see how is the array:
$mysql->ArrayResults($mysql->ExecuteSQL("SELECT * FROM Employee"));
print_r($mysql ->arr2);
Array look weird:
Array ( [0] => Array ( [id] => 1 [first_name] => Jack [last_name] => Schrvaski [start_date] => 1999-07-20 [end_date] => 2011-07-21 [salary] => 1224.56 [city] => Toronto [description] => Programmer ) [1] => Array ( [id] => 2 [first_name] => David [last_name] => Moron [start_date] => 1999-07-20 [end_date] => 2011-07-21 [salary] => 1224.56 [city] => Toronto [description] => Designer ) [2] => Array ( [id] => 3 [first_name] => Eric [last_name] => Ron [start_date] => 1998-08-29 [end_date] => 2011-03-21 [salary] => 1114.56 [city] => Toronto [description] => Tester ) [3] => Array ( [id] => 4 [first_name] => Frost [last_name] => Robert [start_date] => 1999-01-10 [end_date] => 2011-07-10 [salary] => 2114.56 [city] => Toronto [description] => Manager ) [4] => Array ( [id] => 5 [first_name] => Robin [last_name] => Wood [start_date] => 2009-12-22 [end_date] => 2012-02-13 [salary] => 1000.00 [city] => Thronhill [description] => System Analyst ) )
I tried to get a single data using:
print_r($mysql ->arr2[1][2]);
shows no output.But if i type :
print_r($mysql ->arr2[1]['first_name']);
I get David.My question is how to set the array so that I can use loop to get output.
like:
print_r($mysql ->arr2[$i][$j]);
Please help.
I am currently working on a blog page and I need to highlight codes.It will grow as time passes so I am thinking in a broad sense.Geshi looks nice codes are readable but draw back is server side.If a page has 3/4 file it may make it delay on the other hand client side is perfect cause it does not make the server busy.
I need your opinion.My website may have over 500 files of code initially so please comment.I don't like to start without a good knowledge.Cause I all ways like to have feedback.
Thanks for your explanations. I know the code is not the best way to get reverse output.You are absolutely right scaiferw, I just wanted to know how it is working under the hood.Thanks to all for your precious time and effort.So basically the above code is similarly as follows:
for ($i = mysql_num_rows($result) - 1; $i >= 0; $i--) {
if (!mysql_data_seek($result, $i)) {
echo "Cannot seek to row $i: " . mysql_error() . "\n";
continue;
}
if (($row = mysql_fetch_assoc($result))) {
echo $row['last_name'] . ' ' . $row['first_name'] . "<br />\n";
}
}
Am I correct?
Thanks for reply.I can write code better than that but this code is in the example page of php.net.The main reason I am trying to understand this code because I was trying to learn mysql_data_seek() functionality.Please explain the code if any one can.
I spent almost whole evening to understand the following code:
<?php
$link = mysql_connect('localhost', 'root', 'root');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db('database_1.1');
if (!$db_selected) {
die('Could not select database: ' . mysql_error());
}
$query = 'SELECT last_name, first_name FROM Employee';
$result = mysql_query($query);
if (!$result) {
die('Query failed: ' . mysql_error());
}
/* fetch rows in reverse order */
for ($i = mysql_num_rows($result) - 1; $i >= 0; $i--) {
if (!mysql_data_seek($result, $i)) {
echo "Cannot seek to row $i: " . mysql_error() . "\n";
continue;
}
if (!($row = mysql_fetch_assoc($result))) {
continue;
}
echo $row['last_name'] . ' ' . $row['first_name'] . "<br />\n";
}
mysql_free_result($result);
?>
The only vital point of the code I did not understand is,
if (!($row = mysql_fetch_assoc($result)))
Why it has a ! cause whenever we fetch data we do
$row = mysql_fetch_assoc($result))
Please help me to understand the code.Please try to explain a bit.
Thanks to all.God bless you.
Thanks Vijay.It was really helpful.You've got another user of your nice blog.
Dear users,
I am new to PHP5. My LAMP is working just fine.I made small database connect scripts and they just worked as it should.Then I tried the following code and run the mysql.php but showing me following error:
Could not run query: Access denied for user 'www-data'@'localhost' (using password: NO)
My user name is root and password is root.Please check to help me.God bless you all.
<?php
/*database.inc.php
*
*/
define($server, "localhost");
define($conn_username, "root");
define($conn_password, "root");
define($database_name, "database_1.1");
?>
I think there is some mistake somewhere here:
<?php
class mysql
{
var $server;
var $conn_username;
var $conn_password;
var $database_name;
var $connection;
var $select;
var $query;
function connect()
{
require "database.inc.php";
$connection = mysql_connect($server,$conn_username,$conn_password);
$select = mysql_select_db($database_name,$connection);
}
function query($query)
{
$result = mysql_query($query);
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
}
function end()
{
mysql_free_result($connection);
}
}
$mysql = new mysql();
$mysql->connect();
$mysql->query("SELECT * FROM Employee");
$mysql->end();
?>
It will be really helpful if anyone can show me how to get the output in the browser.I want to see a row of the table.
You are fetching images based on categoryid passed in link viewproduct_by_category.php?categoryid=1.
So when you click next link url should also have categoryid i.e. viewproduct_by_category.php?categoryid=1&page=2.
Check in your coding whether you are getting it or not.
Thanks for your reply.My codes are fine when I statically put a value on categoryid i.e 1 or 2...but when I send this value from another page and grab it using request it works only for the first page and second page is empty.I know it is not a good idea to put the whole code but for convenience I am posting it here:
$productcat=$_REQUEST['categoryid'];
$str=mysql_query("select *
from product
where product.productid in
(select productcategory.productid
from productcategory where productcategory.productcat='" . $productcat."') and special='No'
")or die(mysql_error());
$totalrecord=mysql_num_rows($str);
$count=0;
$pagesize=3;
$noofpages=$totalrecord/$pagesize;
if (!isset($_REQUEST['start']))
$start=0;
else
$start=$_REQUEST['start'];
$count=$start;
$strrec="select *
from product
where product.productid in
(select productcategory.productid
from productcategory where productcategory.productcat='" . $productcat."') and special='No'
order by productid DESC limit ".$start.",".$pagesize;
$k=0;
$res1=mysql_query($strrec) or die("cannot select product");
while ($row=mysql_fetch_array($res1)) {
$k++;
$count++;
?>
<td width="200" height="18" class=algray>
<a href="../admin/product/viewproduct.php?productid=<? echo $row["productid"] ?>"><font class=tablacks><b><? echo $row["product"]; ?></b></font></a>
<img src="../admin/product/photo/<? echo $row["photo"] ?>" width="100%" height="160"></font><br></td>
<?
if($k%3==0){
?>
</tr>
<? }
}
?>
</table>
</div>
<!------------------------------------------------------------------------------------------------------------>
<?
if($start<>0)
{
$prev=$start-$pagesize;
echo "<a href='./viewproduct_by_category.php?start=".$prev."'><font class=torange><b><< Prev</b></a> ";
}
?>
<font class=tablackb>Page</font> <font class=tablack><?
for ($i=0;$i<$noofpages;$i++)
{
$pageno=$i+1;
$j=($pagesize*$pageno)-$pagesize;
if ($start==0 && $i==0)
{
echo "<font class=tablackb>". $pageno."</b></font> ";
}
else
{
if($start == ($pagesize*($pageno))-$pagesize)
{
echo "<font class=tablack>". $pageno."</font> ";
}
else
{
echo "<a href='./viewproduct_by_category.php?start=".$j."'><font class=tablackb>". $pageno. "<font></a> ";
} …
I am working on a pagination.From the index page it goes with a variable .For an example:
viewproduct_by_category.php?categoryid=1
the value 1 comes from database.So the error is ,first time the page shows up all the images that has the categoryid =1 but second time when ever I press next page it shows nothing but the pagination values like <<1.2.3..>>
I figured out the problem but I have no idea how to fix it cause each time the page loads the same page .For the first time
$_SESSION['productcat']=$_REQUEST['categoryid'];
Gets the value but when it reloads the same poage it gets nothing so How can I preserve the value.
I will be very happy if some one help me with this.
Thanks