Pull Wikipedia data to localhost page

Hi,

I’m trying to code a book browser with a database backend using HTML, phpMyAdmin, and mySQL. Right now the search function creates a list of book titles with hyperlinks leading to individual blank pages. What I want to make happen is somehow transfer the basic information of said books from Wikipedia pages, to the corresponding blank pages created, when the hyperlinks are clicked. My current code is below: if it helps, the hyperlinks are created at the very end of the code. Please help.

– tristanwang99

<?php if (isset($_GET['q'])) { $q = $_GET['q']; } $con = mysqli_connect('localhost','root','1shoeinthesock','panoply_test'); ?>
<head>

    <title>PANOPLY BETA</title>
    <link rel="stylesheet" type="text/css" href="style.css" />
    <script type="text/javascript">
    </script>

    <style>
		.container1 {
			position: absolute;
			top: 40%;
			left: 50%;
			transform: translateX(-50%) translateY(-50%);
		}

        .container2 {
            top: 0;
            transform: translateY(-50%);
        }
		
        body {
            font-family: times;

        }

        h3 {
            margin: 20px 0px 0px 0px;
            padding: 0;
        }

        p {
            margin: 0;
            padding: 0;
        }

        a {
            color: #000000;
            text-decoration: underline;
        }

        a:hover {
            color: #000000;
            text-decoration: none;
        }

    </style>


</head>

<body>
	 
    <div class = "container1" align = "center">
    <font size="36">PANOPLY</font><br><br>
    <form action="index.php" method="GET" id="searchForm" />

        <input type="text" id="searchBar" name="q" placeholder="search for books..." maxlength="25" autocomplete="off" onMouseDown="" onBlur="" /><input type="submit" id="searchBtn" name="search" value="Go" /><br><br>

    </form>
    </div>

    <?php
        
        if(!isset($q)) {
        
        	echo '';
        	
        } else {
        
            $keyword = strtok($q, " ");
 
            $predicate_word = "";
            $predicate_condition = "";
                       
            while ($keyword) {
            
                 $condition = "";
                 
                 switch ($keyword) {
                 
                      case "COMPLEX":
                           $condition = "`COMPLEX?` = 1";
                           break;
                           
                      case "CONVERSATIONAL":
                      	   $condition = "`CONVERSATIONAL?` = 1";
                      	   break;
                      	   
                      case "GLOOMY":
                      	   $condition = "`GLOOMY?` = 1";
                      	   break;
                      	   
                      case "HEARTWRENCHING":
                      	   $condition = "`HEARTWRENCHING?` = 1";
                      	   break;
                      	   
                      case "INFORMATIVE":
                      	   $condition = "`INFORMATIVE?` = 1";
                      	   break;
                      	   
                      case "METAPHORICAL":
                      	   $condition = "`METAPHORICAL?` = 1";
                      	   break;
                      	   
                      case "RIVETING":
                      	   $condition = "`RIVETING?` = 1";
                      	   break;
                      	   
                      case "SURREAL":
                      	   $condition = "`SURREAL?` = 1";
                      	   break;
                      	   
                      case "UNEXPECTED":
                      	   $condition = "`UNEXPECTED?` = 1";
                      	   break;
                      	   
                      case "UPLIFTING":
                      	   $condition = "`UPLIFTING?` = 1";
                      	   break;
                      	   
                      case "SENTIMENTAL":
                      	   $condition = "`SENTIMENTAL?` = 1";
                      	   break;
                      	   
                      case "SPINETINGLING":
                      	   $condition = "`SPINETINGLING?` = 1";
                      	   break;
                      	   
                      case "WITTY":
                      	   $condition = "`WITTY?` = 1";
                      	   break;
                           
                 }
                 
                 if ($condition == "") {
                 
                     if ($predicate_word == "") {
                     
                          $predicate_word = $keyword;
                          
                     } else {
                     
                          $predicate_word = $predicate_word." ".$keyword;
                          
                     }
                     
                 } else {
                 
                     if ($predicate_condition == "") {
                     
                         $predicate_condition = $condition;
                         
                     } else {
                     
                         $predicate_condition = $predicate_condition." AND ".$condition;
                         
                     }
                     
                 }
                 
                 $keyword = strtok(" ");
                 
            }
            
            $query_string = "SELECT * FROM `TABLE 1` WHERE ";
            
            if ($predicate_condition) {
            
                $query_string = $query_string.$predicate_condition;
                
            }
            
            if ($predicate_word) {
            
                if ($predicate_condition) {
                
                    $query_string = $query_string." AND ";
                    
                }
                
                $query_string = $query_string."CONCAT(`TITLE`,`AUTHOR`,`GENRE`) LIKE '%".$predicate_word."%'";
                
            }
            
            echo '<BR>'.$query_string.'<BR>';
            
            $query = mysqli_query($con, $query_string);
            
            $num_rows = mysqli_num_rows($query);
                
    ?>
        
    <p><strong><?php echo $num_rows; ?></strong> results for '<?php echo $q; ?>'</p>

    <?php
        
            while(isset($q) && ($row = mysqli_fetch_array($query))) {
                $title = $row['TITLE'];
                $author = $row['AUTHOR'];
                $genre = $row['GENRE'];
            
                echo '<h3><a href="' . $title . '.php">' . $title . '</h3></a><p> by ' . $author . '. ' .  $genre . '. </p><br />';
        
            }
        
        }
        
    ?>

</body>

Can you add the Forum’s BBCode PHP Tags around your code… Look in the button’s above the editor, look for php and put that around your code, it is just too difficult to read this way.

You can read more about it here: http://www.phphelp.com/forum/beginners-learning-php/all-posters-please-read-first-14564/

Also, please place all of your code into PHP tags. Press the PHP button, it will show the start and ending tags for you. Then, place your code between them. This will show your code with a scroll bar and make your post smaller and easier to read.
Thank you in advance.

Also, a few comments on your code…

First, if you put your categories into a database table, you could use index to handle your search. It would make your code much smaller. As far as pulling info from Wikipedia, you can do that with a simple PHP command. Something loosely like this:
$url = “http://www.wikipedia.com/somepage…”;
$webpage = file_get_contents($url);
Which will grab an entire page from wikipedia. Then, you have to decode page to find out what you want from the data.

Good luck.

Hi,

Thanks for the replies: I added the PHP tags from the forum. Hopefully that takes care of that. Also, I wasn’t quite clear before on what I wanted to transfer from the Wikipedia pages … such basic information for books is usually placed in a box on the top right of the Wiki page (sometimes with the cover of the book included).

– tristanwang99

[php]<?php

if (isset($_GET['q'])) {
	$q = $_GET['q'];
}

$con = mysqli_connect('localhost','root','1shoeinthesock','panoply_test');

?>

<head>

    <title>PANOPLY BETA</title>
    <link rel="stylesheet" type="text/css" href="style.css" />
    <script type="text/javascript">
    </script>

    <style>
		.container1 {
			position: absolute;
			top: 40%;
			left: 50%;
			transform: translateX(-50%) translateY(-50%);
		}

        .container2 {
            top: 0;
            transform: translateY(-50%);
        }
		
        body {
            font-family: times;

        }

        h3 {
            margin: 20px 0px 0px 0px;
            padding: 0;
        }

        p {
            margin: 0;
            padding: 0;
        }

        a {
            color: #000000;
            text-decoration: underline;
        }

        a:hover {
            color: #000000;
            text-decoration: none;
        }

    </style>


</head>

<body>
	 
    <div class = "container1" align = "center">
    <font size="36">PANOPLY</font><br><br>
    <form action="index.php" method="GET" id="searchForm" />

        <input type="text" id="searchBar" name="q" placeholder="search for books..." maxlength="25" autocomplete="off" onMouseDown="" onBlur="" /><input type="submit" id="searchBtn" name="search" value="Go" /><br><br>

    </form>
    </div>

    <?php
        
        if(!isset($q)) {
        
        	echo '';
        	
        } else {
        
            $keyword = strtok($q, " ");
 
            $predicate_word = "";
            $predicate_condition = "";
                       
            while ($keyword) {
            
                 $condition = "";
                 
                 switch ($keyword) {
                 
                      case "COMPLEX":
                           $condition = "`COMPLEX?` = 1";
                           break;
                           
                      case "CONVERSATIONAL":
                      	   $condition = "`CONVERSATIONAL?` = 1";
                      	   break;
                      	   
                      case "GLOOMY":
                      	   $condition = "`GLOOMY?` = 1";
                      	   break;
                      	   
                      case "HEARTWRENCHING":
                      	   $condition = "`HEARTWRENCHING?` = 1";
                      	   break;
                      	   
                      case "INFORMATIVE":
                      	   $condition = "`INFORMATIVE?` = 1";
                      	   break;
                      	   
                      case "METAPHORICAL":
                      	   $condition = "`METAPHORICAL?` = 1";
                      	   break;
                      	   
                      case "RIVETING":
                      	   $condition = "`RIVETING?` = 1";
                      	   break;
                      	   
                      case "SURREAL":
                      	   $condition = "`SURREAL?` = 1";
                      	   break;
                      	   
                      case "UNEXPECTED":
                      	   $condition = "`UNEXPECTED?` = 1";
                      	   break;
                      	   
                      case "UPLIFTING":
                      	   $condition = "`UPLIFTING?` = 1";
                      	   break;
                      	   
                      case "SENTIMENTAL":
                      	   $condition = "`SENTIMENTAL?` = 1";
                      	   break;
                      	   
                      case "SPINETINGLING":
                      	   $condition = "`SPINETINGLING?` = 1";
                      	   break;
                      	   
                      case "WITTY":
                      	   $condition = "`WITTY?` = 1";
                      	   break;
                           
                 }
                 
                 if ($condition == "") {
                 
                     if ($predicate_word == "") {
                     
                          $predicate_word = $keyword;
                          
                     } else {
                     
                          $predicate_word = $predicate_word." ".$keyword;
                          
                     }
                     
                 } else {
                 
                     if ($predicate_condition == "") {
                     
                         $predicate_condition = $condition;
                         
                     } else {
                     
                         $predicate_condition = $predicate_condition." AND ".$condition;
                         
                     }
                     
                 }
                 
                 $keyword = strtok(" ");
                 
            }
            
            $query_string = "SELECT * FROM `TABLE 1` WHERE ";
            
            if ($predicate_condition) {
            
                $query_string = $query_string.$predicate_condition;
                
            }
            
            if ($predicate_word) {
            
                if ($predicate_condition) {
                
                    $query_string = $query_string." AND ";
                    
                }
                
                $query_string = $query_string."CONCAT(`TITLE`,`AUTHOR`,`GENRE`) LIKE '%".$predicate_word."%'";
                
            }
            
            echo '<BR>'.$query_string.'<BR>';
            
            $query = mysqli_query($con, $query_string);
            
            $num_rows = mysqli_num_rows($query);
                
    ?>
        
    <p><strong><?php echo $num_rows; ?></strong> results for '<?php echo $q; ?>'</p>

    <?php
        
            while(isset($q) && ($row = mysqli_fetch_array($query))) {
                $title = $row['TITLE'];
                $author = $row['AUTHOR'];
                $genre = $row['GENRE'];
            
                echo '<h3><a href="' . $title . '.php">' . $title . '</h3></a><p> by ' . $author . '. ' .  $genre . '. </p><br />';
        
            }
        
        }
        
    ?>

</body>
[/php]

OMG… I think it might be a really GREAT idea to remove the username and password from the script… Especially since it is your root account… Whenever you are posting your code on a public website, try to do this:
[php]
$con = mysqli_connect(‘HOST’,‘UN’,‘PW’,‘DBN’);
[/php]

People here will get it… Hackers will use it :smiley:

Got it. Thanks for the tip.

Some further into… If you are talking about the side panels on the right-side such as in this page:
https://en.wikipedia.org/wiki/The_Old_Man_and_the_Sea
Note on the right is a nice square panel with info about this book. This is easy to find in the source for the page.
It starts with <table class=“infobox vcard” and ends with …

It would be very easy to pull this side-bar table out of their page with just a few lines of code. You can load the page using the code I mentioned before. Then locate this table and save it as you need to for future use.

If that is what you are thinking of doing, I can give you sample code for that process. It is easy.

Hi,

Yes the side panel is exactly what I was referring to. I will try to find and use the code you mentioned in the source of the pages. In the meantime, if you could provide some sample code that would also be very helpful. Thanks.

– tristanwang99

Example, not tested, can test it when I get home, but, not for hours… Read up on the functions.
[php]
$url = “https://en.wikipedia.org/wiki/The_Old_Man_and_the_Sea”;
$webpage = file_get_contents($url);
// Locate start of side panel…
$start = strpos($webpage, “<table class=“infobox vcard””;
// Locate end of side panel…
$end = strpos($webpage, “”, $start); // starts it’s search at beginning of table looking for end tag
// Extract the data which will be the entire table.
// Note this uses the length of the table created from end-of-table minus start-of-table plus length of tag.
$sidebox = substr($webpage, $start, $end-$start+7);
echo $sidebox;
[/php]
I created this off the top of my head, so not tested. Since it is a table, you might have to set the width of the area you display it in to be wide enough. You may also need to take out the links inside of it if you do not have the permission to display them!

Hope that gets you started…

Sponsor our Newsletter | Privacy Policy | Terms of Service