Advertisement:

Author Topic: Generate sitemap via PHP, variable error!  (Read 2111 times)

Online Rock Lee

  • Native Language Support Specialist
  • SMF Hero
  • *
  • Posts: 2,524
  • Gender: Male
  • I also speak english :D
    • BomberCode.Oficial on Facebook
    • RockLee-BC on GitHub
    • @Bomber_Code on Twitter
    • Bomber Code ~ La nueva era del conocimiento
Generate sitemap via PHP, variable error!
« on: November 11, 2018, 09:14:14 AM »
I am trying to adapt this code to generate a PHP sitemap to be updated as requested. I have been repairing the errors but I can not solve this "$message = array();" it marks me as "unexpected error T_VARIABLE" I have tried several ways but could not solve it. Would someone have an idea?

Code: [Select]
<?php 
require_once('./SSI.php');

if (
$modSettings['smfVersion'] < '2.0.15')
ob_start('ob_sessrewrite');

header('Content-Type: text/plain');

$result $smcFunc['db_query']('''
SELECT t.id_topic as id_topic, m.poster_time as poster_time, m.id_msg as id_msg
FROM {db_prefix}topics as t
LEFT JOIN {db_prefix}messages as m ON (t.id_last_msg = m.id_msg)
LEFT JOIN {db_prefix}boards as b ON (t.id_board = b.id_board)
WHERE t.id_board = {int:id_board}
AND {query_see_board}
ORDER BY m.poster_time DESC
LIMIT 49999'
)

$message = array();
while ($row $smcFunc['db_fetch_assoc']($request)) {
$message[] = array(
'id' => $row['id_topic'],
'id_msg' => $row['id_msg'],
'time' => $row['poster_time'],
)
    }
    
$smcFunc['db_free_result']($request);

$request $smcFunc['db_query']('''
SELECT b.id_board as id_board, m.poster_time as poster_time
FROM {db_prefix}boards as b
LEFT JOIN {db_prefix}messages as m on (b.id_last_msg = m.id_msg)
WHERE t.id_board = {int:board}
AND {query_see_board}
ORDER BY m.poster_time DESC
LIMIT 1000'
    
)

$boards = array();
while ($row $smcFunc['db_fetch_assoc']($request))
{
$boards[] = array(
'id' => $row['id_board'],
'time' => $row['poster_time'],
);
}

$smcFunc['db_free_result']($request);
    
echo '<?xml version="1.0" encoding="UTF-8"?' '><urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9/">';
    
    echo 
'
<url>
<loc>'
$scripturl'</loc>
<lastmod>'
date_iso8601(), '</lastmod>
<changefreq>always</changefreq>
<priority>1.0</priority>
</url>'
;

foreach (
$boards as $board)
{
echo '
<url>
<loc>'
$scripturl'?board=' $board '.0</loc>
<lastmod>'
date_iso8601($board['time']), '</lastmod>
<changefreq>always</changefreq>
<priority>1.0</priority>
</url>'
;
}
foreach (
$messages as $message)
{
echo '
<url>
<loc>'
$scripturl'topic=' $topic '.msg' $_REQUEST['msg'] . '#msg' $_REQUEST['msg'], '</loc>
<lastmod>'
date_iso8601($message['time']), '</lastmod>
</url>'
;
}

echo 
'
</urlset>'
;

function 
date_iso8601($timestamp '') {
$timestamp = empty($timestamp) ? time() : $timestamp;
$gmt =  substr(date("O"$timestamp), 03).':00';
return date('Y-m-d\TH:i:s',$timestamp).$gmt;
}
?>


Regards!
¡Regresando como cual Fenix! ~ Bomber Code © 2018
Ayudas - Aportes - Tutoriales - Y mucho mas!!!


Ayudame via PayPal

Offline Shambles

  • SMF Hero
  • ******
  • Posts: 5,104
  • Gender: Male
    • i30 Owners Club
Re: Generate sitemap via PHP, variable error!
« Reply #1 on: November 11, 2018, 09:16:09 AM »
You omitted a semicolon

Code: [Select]
ORDER BY m.poster_time DESC
LIMIT 49999'
);


Online Rock Lee

  • Native Language Support Specialist
  • SMF Hero
  • *
  • Posts: 2,524
  • Gender: Male
  • I also speak english :D
    • BomberCode.Oficial on Facebook
    • RockLee-BC on GitHub
    • @Bomber_Code on Twitter
    • Bomber Code ~ La nueva era del conocimiento
Re: Generate sitemap via PHP, variable error!
« Reply #2 on: November 11, 2018, 09:22:28 AM »
Now he tells me: syntax error, unexpected '}' it seems it is by the while although looking at the documentation is the correct way in theory.
¡Regresando como cual Fenix! ~ Bomber Code © 2018
Ayudas - Aportes - Tutoriales - Y mucho mas!!!


Ayudame via PayPal

Offline Shambles

  • SMF Hero
  • ******
  • Posts: 5,104
  • Gender: Male
    • i30 Owners Club
Re: Generate sitemap via PHP, variable error!
« Reply #3 on: November 11, 2018, 09:23:10 AM »
You have other errors too:

use this instead:

Code: [Select]
<?php 
require_once('./SSI.php');

if (
$modSettings['smfVersion'] < '2.0.15')
ob_start('ob_sessrewrite');

header('Content-Type: text/plain');

$result $smcFunc['db_query']('''
SELECT t.id_topic as id_topic, m.poster_time as poster_time, m.id_msg as id_msg
FROM {db_prefix}topics as t
LEFT JOIN {db_prefix}messages as m ON (t.id_last_msg = m.id_msg)
LEFT JOIN {db_prefix}boards as b ON (t.id_board = b.id_board)
WHERE t.id_board = {int:id_board}
AND {query_see_board}
ORDER BY m.poster_time DESC
LIMIT 49999'
);

$message = array();
while ($row $smcFunc['db_fetch_assoc']($request)) 
$message[] = array(
'id' => $row['id_topic'],
'id_msg' => $row['id_msg'],
'time' => $row['poster_time'],
);
   
    
$smcFunc['db_free_result']($request);

$request $smcFunc['db_query']('''
SELECT b.id_board as id_board, m.poster_time as poster_time
FROM {db_prefix}boards as b
LEFT JOIN {db_prefix}messages as m on (b.id_last_msg = m.id_msg)
WHERE t.id_board = {int:board}
AND {query_see_board}
ORDER BY m.poster_time DESC
LIMIT 1000'
    
);

$boards = array();
while ($row $smcFunc['db_fetch_assoc']($request))
{
$boards[] = array(
'id' => $row['id_board'],
'time' => $row['poster_time'],
);
}

$smcFunc['db_free_result']($request);
    
echo '<?xml version="1.0" encoding="UTF-8"?' '><urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9/">';
    
    echo 
'
<url>
<loc>'
$scripturl'</loc>
<lastmod>'
date_iso8601(), '</lastmod>
<changefreq>always</changefreq>
<priority>1.0</priority>
</url>'
;

foreach (
$boards as $board)
{
echo '
<url>
<loc>'
$scripturl'?board=' $board '.0</loc>
<lastmod>'
date_iso8601($board['time']), '</lastmod>
<changefreq>always</changefreq>
<priority>1.0</priority>
</url>'
;
}
foreach (
$messages as $message)
{
echo '
<url>
<loc>'
$scripturl'topic=' $topic '.msg' $_REQUEST['msg'] . '#msg' $_REQUEST['msg'], '</loc>
<lastmod>'
date_iso8601($message['time']), '</lastmod>
</url>'
;
}

echo 
'
</urlset>'
;

function 
date_iso8601($timestamp '') {
$timestamp = empty($timestamp) ? time() : $timestamp;
$gmt =  substr(date("O"$timestamp), 03).':00';
return date('Y-m-d\TH:i:s',$timestamp).$gmt;
}
?>

Offline vbgamer45

  • SMF Friend
  • SMF Super Hero
  • *
  • Posts: 21,187
    • smfhacks on Facebook
    • VBGAMER45 on GitHub
    • @createaforum on Twitter
    • SMF For Free
Re: Generate sitemap via PHP, variable error!
« Reply #4 on: November 11, 2018, 09:55:58 AM »
I just use this mod https://custom.simplemachines.org/mods/index.php?mod=755 still works on 2.0.x
Community Suite for SMF - Take your forum to the next level built for SMF, Gallery,Store,Classifieds,Downloads,more!

SMFHacks.com -  Paid Modifications for SMF

Mods:
EzPortal - Portal System for SMF
SMF Gallery Pro
SMF Store SMF Classifieds Ad Seller Pro

Online Rock Lee

  • Native Language Support Specialist
  • SMF Hero
  • *
  • Posts: 2,524
  • Gender: Male
  • I also speak english :D
    • BomberCode.Oficial on Facebook
    • RockLee-BC on GitHub
    • @Bomber_Code on Twitter
    • Bomber Code ~ La nueva era del conocimiento
Re: Generate sitemap via PHP, variable error!
« Reply #5 on: November 11, 2018, 06:57:03 PM »
@Sh@mblesYou can see my mistakes now, it marks me the following which is an advance, it will be a question to see how I fix it. Thank you!

Code: [Select]
<div id="fatal_error">
<div class="cat_bar">
<h3 class="catbg">
¡Un error ha ocurrido!
</h3>
</div>
<div class="windowbg">
<span class="topslice"><span></span></span>
<div class="padding">The database value you're trying to insert does not exist: id_board</div>
<span class="botslice"><span></span></span>
</div>
</div>
<div class="centertext"><a href="javascript:history.go(-1)">Atrás</a></div>

And @vbgamer45 I did not remember it, I'm going to look at it, although what I want to achieve is from the main domain, and not from a subfolder. For example www.Mydomain.com/sitemap and not www.Mydomain.com/forum/sitemap but I will try the way you comment, thanks for the suggestion.


Regards!
¡Regresando como cual Fenix! ~ Bomber Code © 2018
Ayudas - Aportes - Tutoriales - Y mucho mas!!!


Ayudame via PayPal

Offline Shambles

  • SMF Hero
  • ******
  • Posts: 5,104
  • Gender: Male
    • i30 Owners Club
Re: Generate sitemap via PHP, variable error!
« Reply #6 on: November 11, 2018, 07:39:43 PM »
Quote from: Rock Lee
You can see my mistakes now, it marks me the following which is an advance, it will be a question to see how I fix it.

Sorry, I've no idea what you're trying to tell/ask me.

Online Rock Lee

  • Native Language Support Specialist
  • SMF Hero
  • *
  • Posts: 2,524
  • Gender: Male
  • I also speak english :D
    • BomberCode.Oficial on Facebook
    • RockLee-BC on GitHub
    • @Bomber_Code on Twitter
    • Bomber Code ~ La nueva era del conocimiento
Re: Generate sitemap via PHP, variable error!
« Reply #7 on: November 11, 2018, 07:59:57 PM »
Sorry, I've no idea what you're trying to tell/ask me.

That you helped me a lot by making me realize my mistakes, now I have to fix to the "id_board" that I'm doing wrong, I think. The line gives me those problems is:  "WHERE t.id_board = {int:id_board}" I have copied some answers but I can not make it work.
¡Regresando como cual Fenix! ~ Bomber Code © 2018
Ayudas - Aportes - Tutoriales - Y mucho mas!!!


Ayudame via PayPal

Offline SychO

  • Customizer
  • Sophist Member
  • *
  • Posts: 1,189
  • Gender: Male
    • SychO9 on GitHub
    • SychO
Re: Generate sitemap via PHP, variable error!
« Reply #8 on: November 12, 2018, 05:58:09 PM »
you have to give id_board from {int:id_board} a value in the query parameters, something like this

Code: [Select]
<?php

$result 
$smcFunc['db_query']('''
SELECT t.id_topic as id_topic, m.poster_time as poster_time, m.id_msg as id_msg
FROM {db_prefix}topics as t
LEFT JOIN {db_prefix}messages as m ON (t.id_last_msg = m.id_msg)
LEFT JOIN {db_prefix}boards as b ON (t.id_board = b.id_board)
WHERE t.id_board = {int:id_board}
AND {query_see_board}
ORDER BY m.poster_time DESC
LIMIT 49999'
,
array(
'id_board' => 1,
)
);

Imazighen

Online Rock Lee

  • Native Language Support Specialist
  • SMF Hero
  • *
  • Posts: 2,524
  • Gender: Male
  • I also speak english :D
    • BomberCode.Oficial on Facebook
    • RockLee-BC on GitHub
    • @Bomber_Code on Twitter
    • Bomber Code ~ La nueva era del conocimiento
Re: Generate sitemap via PHP, variable error!
« Reply #9 on: November 12, 2018, 08:07:55 PM »
If I use it with the array, it returns me "Unknown column 't.id_board' in 'where clause'" and if I do not use the array it returns "The database value you're trying to insert does not exist: id_board" ... I'm not sure why that mistake marks me.


Regards!
¡Regresando como cual Fenix! ~ Bomber Code © 2018
Ayudas - Aportes - Tutoriales - Y mucho mas!!!


Ayudame via PayPal

Offline Shambles

  • SMF Hero
  • ******
  • Posts: 5,104
  • Gender: Male
    • i30 Owners Club
Re: Generate sitemap via PHP, variable error!
« Reply #10 on: November 13, 2018, 05:30:00 AM »
What about the other query that you have?

Code: [Select]
$request = $smcFunc['db_query']('', '
SELECT b.id_board as id_board, m.poster_time as poster_time
FROM {db_prefix}boards as b
LEFT JOIN {db_prefix}messages as m on (b.id_last_msg = m.id_msg)
WHERE t.id_board = {int:board}
AND {query_see_board}
ORDER BY m.poster_time DESC
LIMIT 1000'
    );

You haven't assigned a table to "t" nor are you passing in the board id.

Online Rock Lee

  • Native Language Support Specialist
  • SMF Hero
  • *
  • Posts: 2,524
  • Gender: Male
  • I also speak english :D
    • BomberCode.Oficial on Facebook
    • RockLee-BC on GitHub
    • @Bomber_Code on Twitter
    • Bomber Code ~ La nueva era del conocimiento
Re: Generate sitemap via PHP, variable error!
« Reply #11 on: November 13, 2018, 09:19:10 AM »
I was trying to change it in both I get an error even though I think this "t." this bad I was copying from the SMF files because I thought it would work the way I wish but not. The original code is:

Code: [Select]
$request = db_query("
SELECT t.ID_TOPIC as ID_TOPIC, m.posterTime as posterTime, m.ID_MSG as ID_MSG
FROM {$db_prefix}topics as t
LEFT JOIN {$db_prefix}messages as m ON (t.ID_LAST_MSG = m.ID_MSG)
LEFT JOIN {$db_prefix}boards as b ON (t.ID_BOARD = b.ID_BOARD)
WHERE $user_info[query_see_board]
ORDER BY m.posterTime DESC
LIMIT 49999", __FILE__, __LINE__);

$messages = array();
while ($row = mysql_fetch_assoc($request))
{
$messages[] = array(
'id' => $row['ID_TOPIC'],
'ID_MSG' => $row['ID_MSG'],
'time' => $row['posterTime'],
);
}
mysql_free_result($request);

$request = db_query("
SELECT b.ID_BOARD as ID_BOARD, m.posterTime as posterTime
FROM {$db_prefix}boards as b
LEFT JOIN {$db_prefix}messages as m on (b.ID_LAST_MSG = m.ID_MSG)
WHERE $user_info[query_see_board]
ORDER BY m.posterTime DESC
LIMIT 1000", __FILE__, __LINE__);

It works perfectly with the 1.x branch but not with the 2.0.x so I want to see if I update. Although it may be because of the structure change, I am calling the variables wrong ???.


Regards!
¡Regresando como cual Fenix! ~ Bomber Code © 2018
Ayudas - Aportes - Tutoriales - Y mucho mas!!!


Ayudame via PayPal

Offline SychO

  • Customizer
  • Sophist Member
  • *
  • Posts: 1,189
  • Gender: Male
    • SychO9 on GitHub
    • SychO
Re: Generate sitemap via PHP, variable error!
« Reply #12 on: November 13, 2018, 10:53:58 AM »
if you're gonna remove the FROM {db_prefix}topics as t part then you have to edit the rest and use something other than t. depending on what you're trying to query

Imazighen

Online Rock Lee

  • Native Language Support Specialist
  • SMF Hero
  • *
  • Posts: 2,524
  • Gender: Male
  • I also speak english :D
    • BomberCode.Oficial on Facebook
    • RockLee-BC on GitHub
    • @Bomber_Code on Twitter
    • Bomber Code ~ La nueva era del conocimiento
Re: Generate sitemap via PHP, variable error!
« Reply #13 on: November 13, 2018, 11:55:19 AM »
if you're gonna remove the FROM {db_prefix}topics as t part then you have to edit the rest and use something other than t. depending on what you're trying to query

I have some doubts although I am reading the documentation to see if I can understand the fault I have, but I still have no luck.


Regards!
¡Regresando como cual Fenix! ~ Bomber Code © 2018
Ayudas - Aportes - Tutoriales - Y mucho mas!!!


Ayudame via PayPal