Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear Experts,

Could you please provide sample PHP coding for following url? It will be a great help. Thanks!

http://power.arc.losrios.edu/~leeverc/php/booksassignment/books.php?choice=genre[^]
Posted
Comments
enhzflep 9-Nov-13 23:54pm    
At CodeProject this question is unlikely to be considered reasonable.
What have you tried, where are you stuck?

Break the problem down into manageable chunks and solve them one by one.
There are many chunks or pieces involved in this question, there are also many tutorials that will address some if not all of them.
abdulsafran 10-Nov-13 9:39am    
Thanks enhzflep, I'll check this and if there any problem. I'll get back to you.
enhzflep 10-Nov-13 11:55am    
You're welcome abdulsafran.

I don't hand-out fish when asked, though I do try to teach people to fish. If someone asks to be taught how to fish, it's not uncommon that I'll give them a fish while I'm at it.

Networking with people is important. As a software developer, the ability to research is paramount. You can get answers by asking people, but understanding and true mastery comes from an inquiring mind.

Seek and you shall find. When faced with a specific problem, come back and ask - most here will really try hard to help you overcome a hurdle. But they wont carry your through the whole race.

I hope you understand there are no hard feelings.

Best regards. :)
abdulsafran 10-Nov-13 14:37pm    
Hi enhzflep, Below is my code. Once click the Title, Address bar of the browser will be changed to books.php?choice=title. My problem is I need to run sort this table from title, so I want to add sort title php code inside this single php file. I need like header = books.php?choice=title then run the code else below code. Please help me.
abdulsafran 10-Nov-13 14:38pm    
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM authors a, books b, publish p where p.authorid=a.authorid and p.bookid=b.bookid");

echo "<table border='1'>
<tr><th colspan='7'>Add Book |
Add Author</td></tr>
<tr>
<th>Title</th>
<th>Author Name</th>
<th>Genre</th>
<th>Country</th>
</tr>";

while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['fname'] . "</td>";
echo "<td>" . $row['genre'] . "</td>";
echo "<td>" . $row['country'] . "</td>";
echo "</tr>";
}
echo "</table>";

mysqli_close($con);
?>

1 solution

Alright then. Since I seem to be doing more work on this than you do, I'll pass you this code and leave it at that. You can adapt it from now on to suit your own needs.

There's all kinds of ways you could do it. I just realized the most obvious way as I was about to paste a much larger snippet. Oh well, :sighs: mental gymnastics are good for us! Naturally, you could remove each instance of "\t\t\t\t" - you could also un-indent the other html tags too. I just prefer nice indentation (although it does make the page a little larger - for no benefit unless you view-source)

Any-who..

XML
<!DOCTYPE html>
<html>
    <head>
        <style>
            tr.sortStuff th:hover{background-color: rgb(200,200,200);}
            tr.addStuff th:hover{background-color: rgb(200,255,200);}
        </style>
        <script>
        </script>
    </head>
    <body>
    <table>
        <thead>
            <tr class='addStuff'>
                <th colspan='7'>Add Book | Add Author</th>
            </tr>

            <tr class='sortStuff'>
                <th><a href='<?php echo $_SERVER["PHP_SELF"]?>?order=title'>Title</th>
                <th><a href='<?php echo $_SERVER["PHP_SELF"]?>?order=author'>Author Name</th>
                <th><a href='<?php echo $_SERVER["PHP_SELF"]?>?order=genre'>Genre</th>
                <th><a href='<?php echo $_SERVER["PHP_SELF"]?>?order=country'>Country</th>
            </tr>
        </thead>
        <tbody>
<?php
/*
$sortField = $_GET['order'];
if (mysqli_connect_errno())
  {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
  $sqlStr = "SELECT * FROM authors a, books b, publish p where p.authorid=a.authorid and p.bookid=b.bookid";
  if (isset($sortField))
    $sqlStr += " ORDER BY $sortField ASC";
  $result = mysqli_query($con, $sqlStr);

while($row = mysqli_fetch_array($result))
  {
  echo "\t\t\t\t<tr>";
  echo "\t\t\t\t<td>" . $row['title'] . "</td>";
  echo "\t\t\t\t<td>" . $row['fname'] . "</td>";
  echo "\t\t\t\t<td>" . $row['genre'] . "</td>";
  echo "\t\t\t\t<td>" . $row['country'] . "</td>";
  echo "\t\t\t\t</tr>";
  }
mysqli_close($con);
*/
?>      </tbody>
    </table>
    </body>
</html>
 
Share this answer
 
Comments
abdulsafran 11-Nov-13 6:49am    
Thank you very much for your hard work. Now I'm testing the code, there is an error as Undefined index at $sortField = $_GET['order']; always. Without that code we can't sort it, what is the problem? I'm trying.
enhzflep 11-Nov-13 9:17am    
You're welcome - we each benefit from it, I think.

What do you mean by "Undefined index at $sortField = $_GET['order']; always"?

I did test the code before posting and apart from the SQL stuff, (which I obviously commented out, since I dont have the database, nor the inclination to create one) It worked as expected.
Let me just try to copy the code from this page to check - yeah works okay. I'll see if I can hook it up to a database while I await your reply.

Where are you seeing that error message? On the screen, in the Browser console?
And, just for the sake of clarity - was that the exact message?
abdulsafran 11-Nov-13 9:56am    
Error is Notice: Undefined index: order in D:\Program Files\wamp\www\BooksAndAuthors\Test.php on line 30. Line 30 is $sortField = $_GET['order'];. Once I commented that line, data is displaying table but sorting is not working.
enhzflep 11-Nov-13 10:23am    
Ahh. I'm using XAMPP, perhaps the version of php that we each have is different.

In any case, you can test if this is the problem by starting with a sort url, i.e
rather than "/localhost/somePath/mySort.php", try "/localhost/somePath/mySort.php?order=title"

It seems like it may be failing since the $_GET array doesn't contain an entry with the index of 'order'. - entering in a url that you know will pass this parameter should test this.

If that does make it work, just re-jig the way we use isset and then grab the value of $_GET['order'].



Change from:
$sortField = $_GET['order'];
if (isset($sortField))
$sqlStr += " ORDER BY $sortField ASC";

To:
if (isset($_GET['order']))
$sqlStr += " ORDER BY $_GET['order'] ASC";
abdulsafran 11-Nov-13 13:16pm    
I have changed mentioned lines like below to set with PHP.

if (isset($_GET['order']))
$sqlStr += " ORDER BY " . $_GET['order'] . "ASC";

But now, table is displaying, once click the title or other table headings, following error is occurring: Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in D:\Program Files\wamp\www\BooksAndAuthors\Test.php on line 42.

Line 42 is: while($row = mysqli_fetch_array($result))

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900